我有一个页面来选择要上传的csv文件,还有一个导入页面来循环浏览csv并将这些记录添加到数据库中。我从https://www.phpflow.com/php/import-csv-file-into-mysql/复制并修改了脚本,文件选择页面很好,并且导入进入循环,在该循环中我回显一个单元格以查看数据以确保其正常工作。问题是文件有45行,脚本不打印任何内容,我添加和显示的计数器将在15秒内达到150000。基本上,它循环执行而不会退出或读取文件。
if(isset($_POST["myfile"])){
$host="localhost"; // Host name.
$db_user="root"; //mysql user
$db_password="password"; //mysql pass
$db='mydb'; // Database name.
//$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
//mysql_select_db($db) or die (mysql_error());
$conn=mysqli_connect($host,$db_user,$db_password,$db);
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$filename=$_FILES["file"]["name"];
//Below is original line
//$ext=substr($filename,strrpos($filename,"."),(strlen($filename)-strrpos($filename,".")));
$ext=substr($_FILES["file"]["name"],-3);
if($ext=="csv"){
$file = fopen($filename, "r");
$cnt=0;
while (($row = fgetcsv($file, 0, ",")) !== FALSE) {
echo $cnt=$cnt+1 . ". ";
echo $row[5] . "<br>";
ob_flush();
//echo fgets($file). "<br />";
}
fclose($file);
echo "CSV File has been successfully Imported.";
}
else {
echo "Error: Please Upload only CSV File";
}
}
它应该显示“ 1. Field5”,而当我停止脚本时,无论计数器到什么,它都只显示“ 1.”。为什么我的脚本不读取文件?