未显示 CSV 错误消息

CSV error message not shown

我可以上传和覆盖我的 CSV 文件中的数据。 但是,当我上传错误的文件类型(例如上传 PPT 文件)时,错误 消息 "Invalid File. Please check that the file is uploaded in .CSV format!" 未显示。但好消息是我的本地主机数据库也没有更新(因为它不是正确的文件) 我应该如何编辑代码以使错误成为 shown/prompted?

 <?php
 session_start();
 if (isset($_POST["Upload"])) {
include "dbFunctions.php";
mysql_select_db($DB) or die(mysql_error());
$filename = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
    $file = fopen($filename, "r");


    $count = 0;
    while (($excelData = fgetcsv($file, 10000, ",")) !== FALSE) {
        $count++;

        if ($count > 1) {
            $sql = "INSERT into abc (id, password, name, dob, contact_number, email, gender, module, school) values ('" . $excelData[0] . "',SHA1('" . $excelData[1] . "'),'" . $excelData[2] . "','" . $excelData[3] . "','" . $excelData[4] . "','" . $excelData[5] . "','" . $excelData[6] . "','" . $excelData[7] . "','" . $excelData[8] . "') ON DUPLICATE KEY UPDATE password=SHA1('" . $excelData[1] . "'), name='$excelData[2]', dob='$excelData[3]', contact_number='$excelData[4]', email='$excelData[5]', gender='$excelData[6]', module='$excelData[7]', school='$excelData[8]'";
            mysql_query($sql);

            $msg = 'CSV File has been successfully imported';
        } else {
            $msg = 'Invalid File. Please check that the file is uploaded in .CSV format!';
             }
         }
     }
 }
 ?>

 <!DOCTYPE html>
 <html>
     <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>**** System</title>
         <link href="stylesheet/style.css" rel="stylesheet"/>
     </head>
     <body>
     <?php
    include "navigationBar.php";
    ?>

    <h2>Load Details</h2>
    <?php
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    ?>
    <br>
    <?php
    echo $msg
    ?>

</body>

 </html>     

您尝试在从文件读取数据的循环中显示错误。

尝试使用 do {...} while(...) 循环来捕获错误:

$count = 0;
do {
   $excelData = fgetcsv($file, 10000, ",");

   if ($excelData === FALSE && $count == 0) {
      //stop importing if first returned data is FALSE
      break;
   }
   elseif ($excelData !== FALSE) {
      $count++;
      $sql = "INSERT into abc (id, password, name, dob, contact_number, email, gender, module, school) values ('" . $excelData[0] . "',SHA1('" . $excelData[1] . "'),'" . $excelData[2] . "','" . $excelData[3] . "','" . $excelData[4] . "','" . $excelData[5] . "','" . $excelData[6] . "','" . $excelData[7] . "','" . $excelData[8] . "') ON DUPLICATE KEY UPDATE password=SHA1('" . $excelData[1] . "'), name='$excelData[2]', dob='$excelData[3]', contact_number='$excelData[4]', email='$excelData[5]', gender='$excelData[6]', module='$excelData[7]', school='$excelData[8]'";
      mysql_query($sql);         
   } 
} while ($excelData !== FALSE);

// display message depending on number of rows imported
if ($count > 0) {
   $msg = 'CSV File has been successfully imported';
}
else {
   $msg = 'Invalid File. Please check that the file is uploaded in .CSV format!';
}
echo $msg;

您可以(或者甚至应该)在上传文件之前检查文件的扩展名。当然,扩展名 并不 表示文件中的数据格式正确,但它 可能是一个提示 ,如果你没有适当的扩展,数据可以是 not-csv-like.

要检查文件扩展名,只需使用:

if (isset($_FILES['file']) && strcasecmp('csv', pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)) === 0) {
   // file extension is CSV 
} else {
   // file extension is NOT CSV
}