isg
isg

Reputation: 39

CSV error message not shown

I am able to upload and overwrite the data from my CSV file. But when I upload the wrong file type (example upload PPT file), the error message "Invalid File. Please check that the file is uploaded in .CSV format!" is not shown. But the good thing is my localhost database did not update too (since it's not the right file) How should I edit the code such that the error will be 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>     

Upvotes: 0

Views: 56

Answers (1)

Kleskowy
Kleskowy

Reputation: 2668

You try to show an error in a loop that reads data from file.

Try using do {...} while(...) loop to catch an error:

$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;

You could (or possibly even should) check file's extension before it's uploaded. Of course the extension does not mean the data inside a file is properly formatted, but it could be a hint, that if you don't have the proper extenstion, the data could be not-csv-like.

To check file extension simpy use:

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

Upvotes: 2

Related Questions