我必须编写一个代码,我需要将他们名字的人的电子邮件ID导入数据库中的excel表,但面临的问题是它显示我无效的文件,其中文件位于.csv格式,请帮忙,对这个概念不熟悉,请原谅我,如果我在某个地方出错了。
import.php
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="sel_file" size="20" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
include ("connection.php");
if(isset($_POST["submit"]))
{
$fname = $_FILES['sel_file']['tmp_name'];
echo'Upload file name is'.$fname.' ';
$chk_ext = explode(".",$fname);
if(strtolower(end($chk_ext)) == "csv"){
$filename = $_FILES['sel_file'] ['tmp_name'];
$handle = fopen($filename, "r");
while(($data = fgetcsv($handle, 1000, ",")) !== false)
{
$sql = "INSERT into import_email (vault_no, name, email) values ('".$_SESSION['vault_no']."', '$data[0]', '$data[1]')";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
echo "Successfully imported! ";
}else{
echo "Invalid file!";
}
}
?>
答案 0 :(得分:0)
您正在使用临时名称而不是名称
$_FILES['sel_file'] ['tmp_name'];
将其更改为:
$_FILES['sel_file'] ['name'];
$_FILE['input_file']
的例子是
[input_file] => Array
(
[name] => MyFile.jpg //<-------- This is the one you should use because it contains the extension (that you are checking for)
[type] => image/jpeg
[tmp_name] => /tmp/php/php6hst32 //<----------- this is the one you used
[error] => UPLOAD_ERR_OK
[size] => 98174
)
所以你的PHP部分应该是这样的:
<?php
include("connection.php");
if (isset($_POST["submit"])) {
$fname = $_FILES['sel_file']['name']; // Changed only this
echo 'Upload file name is' . $fname . ' ';
$chk_ext = explode(".", $fname);
if (strtolower(end($chk_ext)) == "csv") {
$filename = $_FILES['sel_file'] ['tmp_name'];
$handle = fopen($filename, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$sql = "INSERT into import_email (vault_no, name, email) values ('" . $_SESSION['vault_no'] . "', '$data[0]', '$data[1]')";
mysql_query($sql) or die(mysql_error());
}
fclose($handle);
echo "Successfully imported! ";
} else {
echo "Invalid file!";
}
}
?>