0

I am getting an error while inserting although my filename and location are properly set..Any solution? Thanks in advance...Here are my codes:

// path where your CSV file is located
define('CSV_PATH','C:\xampp\htdocs\form');

// Name of your CSV file
$csv_file = CSV_PATH . "test.csv"; 


if (($handle = fopen($csv_file, "r")) !== FALSE) {
   fgetcsv($handle);   
   while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        for ($c=0; $c < $num; $c++) {
          $col[$c] = $data[$c];
        }

 $col1 = $col[0];
 $col2 = $col[1];
 $col3 = $col[2];

 $query = "INSERT INTO csvTable(ID,Name,city) VALUES ('".$col1."','".$col2."','".$col3."')";
 $params = array();
 $options =  array( "Scrollable" => SQLSRV_CURSOR_KEYSET );
 $stmt = sqlsrv_query($conn, $query, $params, $options ); 
 }
    fclose($handle);
 }

echo "File data successfully imported to database!!";
//mysql_close($connect);
?>

Error Message: Warning: fopen(C:\xampp\htdocs\formtest.csv): failed to open stream: No such file or directory in C:\xampp\htdocs\form\upload.php on line 15

Vipin CP
  • 3,642
  • 3
  • 33
  • 55
avi023
  • 11
  • 1
  • 2
  • Perhaps you missed the backslash before your CSV file name ? `$csv_file = CSV_PATH . "\test.csv";` – Servuc Aug 10 '16 at 06:44
  • That's right. And you can quickly find the cause to this kind of problems by following this checklist : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Aug 20 '16 at 08:45

1 Answers1

0

Add backslash('\') after Form in define value

define('CSV_PATH','C:\xampp\htdocs\form');

Change TO

define('CSV_PATH','C:\xampp\htdocs\form\');

OR

Add backslash('\') before your CSV file name

// Name of your CSV file
$csv_file = CSV_PATH . "test.csv"; 

Change to

$csv_file = CSV_PATH . "\test.csv"; 
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38
  • More generally, there is a checklist to troubleshoot this kind of problems : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Aug 20 '16 at 08:45