[SHARE] Upload CSV file and Insert Into MySQL

by 0 replies
0
I want to share tip how to get CSV file and insert into MySQL table.

Code:
if(isset($_POST['submit']))
{
     $fname = $_FILES['fupload']['name']; //get your csv file from local
     $chk_ext = explode(".",$fname);
     if(strtolower($chk_ext[1]) == "csv") //check this file, csv or not?
     {
         $filename = $_FILES['fupload']['tmp_name'];
         $handle = fopen($filename, "r");
         
         while (($data = fgetcsv($handle,1024,",")) !== FALSE)
         {
            $column1 = trim($data[0]);
            $column2 = trim($data[2]);
           
            $sql = "INSERT INTO your_table_name (field1, field2) values('$column1', '$column2')";
         }
         fclose($handle);
     } else {
        echo 'It is not csv file';
     }
}
#programming #csv #file #insert #mysql #share #upload

Next Topics on Trending Feed

  • 0

    I want to share tip how to get CSV file and insert into MySQL table. Code: if(isset($_POST['submit'])) { $fname = $_FILES['fupload']['name']; //get your csv file from local $chk_ext = explode(".",$fname); if(strtolower($chk_ext[1]) == "csv") //check this file, csv or not? { $filename = $_FILES['fupload']['tmp_name']; $handle = fopen($filename, "r"); while (($data = fgetcsv($handle,1024,",")) !== FALSE) { $column1 = trim($data[0]); $column2 = trim($data[2]); $sql = "INSERT INTO your_table_name (field1, field2) values('$column1', '$column2')"; } fclose($handle); } else { echo 'It is not csv file'; } }