Jump to content

[SOLVED] fgetcsv problem


piznac

Recommended Posts

Ok I had this working earlier,.. but wanted to update as well. And now things seem to have gotten screwed up. Any help would be appreciated.::

 

<?php
$base = "/var/www/html/ups/";
$filename = $_FILES['file']['name'];
echo $filename;
if (@is_uploaded_file($_FILES["file"]["tmp_name"])) { 
copy($_FILES["file"]["tmp_name"], "$base" . "$filename"); 
}
if (isset($filename))
   {
     $filenameb= "$base" . "$filename";
     echo $filenameb;
     $handle = fopen($filenameb, "r");
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
$import="INSERT INTO orders(`or_date`,`ship_date`,`ship_via`,`job_name`,`job_desc`,`finishing`,`or_num`,`po_num`,`cust_name`,`tracking`,`pro_date`) values('$data[0]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[1]','$data[2]','$data[3]','','')ON DUPLICATE KEY UPDATE ship_date = VALUES(ship_date),ship_via = VALUES(ship_via),job_name = VALUES(job_name),job_desc = VALUES(job_desc),finishing = VALUES(finishing),po_num = (po_num),cust_name = VALUES(cust_name);";
     mysql_query($import) or die("insert" . mysql_error());
     

     fclose($handle);
     print "Import done";
}
   }else{
   echo "No way hose";
   }

?>

 

This is the error Im getting:

 

Warning: fgetcsv(): 6 is not a valid stream resource in /var/www/html/control/up.php on line 84

 

Now the file is there and is a csv, seperated by a comma. I dont get it.

 

Link to comment
https://forums.phpfreaks.com/topic/57087-solved-fgetcsv-problem/
Share on other sites

Ok permissions look good. And I tried that but Im getting the same error. I might have changed something else in the process,.. here is what I have

 

<?php require_once('sew.php');
require_once('sew.php');
$base = "/var/www/html/ups/";
$filename = $_FILES['file']['name'];

if (@is_uploaded_file($_FILES["file"]["tmp_name"])) { 
copy($_FILES["file"]["tmp_name"], "$base" . "$filename"); 
}
$filenameb= $base . $filename;
if (file_exists($filenameb))
   {
         echo "$filenameb</br>";
     $handle = fopen("$filenameb", "r") or die("no go");
     while (($data = fgetcsv($handle, 50000, "," )) !== FALSE ){
$import="INSERT INTO orders(`or_date`,`ship_date`,`ship_via`,`job_name`,`job_desc`,`finishing`,`or_num`,`po_num`,`cust_name`,`tracking`,`pro_date`) values('$data[0]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[1]','$data[2]','$data[3]','','')ON DUPLICATE KEY UPDATE ship_date = VALUES(ship_date),ship_via = VALUES(ship_via),job_name = VALUES(job_name),job_desc = VALUES(job_desc),finishing = VALUES(finishing),po_num = (po_num),cust_name = VALUES(cust_name);";
     mysql_query($import) or die("insert" . mysql_error());
      
     fclose($handle);
     print "Import done";
}
   }else{
   echo "No way hose";
   }

?>

 

Wait,.. one sec :) ,.. yep still the same thing

ok when I take this out:

 

while (($data = fgetcsv($handle, 50000, "," )) !== FALSE ){
$import="INSERT INTO orders(`or_date`,`ship_date`,`ship_via`,`job_name`,`job_desc`,`finishing`,`or_num`,`po_num`,`cust_name`,`tracking`,`pro_date`) values('$data[0]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[1]','$data[2]','$data[3]','','')ON DUPLICATE KEY UPDATE ship_date = VALUES(ship_date),ship_via = VALUES(ship_via),job_name = VALUES(job_name),job_desc = VALUES(job_desc),finishing = VALUES(finishing),po_num = (po_num),cust_name = VALUES(cust_name);";
     mysql_query($import) or die("insert" . mysql_error());
      
     fclose($handle);
     echo "Import Done";
}

 

and leave just this:

 

<?php 
$base = "/var/www/html/ups/";
$filename = $_FILES['file']['name'];

if (@is_uploaded_file($_FILES["file"]["tmp_name"])) { 
copy($_FILES["file"]["tmp_name"], "$base" . "$filename"); 
}
$filenameb= $base . $filename;
if (file_exists($filenameb)){
     echo "$filenameb</br>";
     $handle = fopen("$filenameb", "r") or die("no go");
   $data = fgetcsv($handle, 50000, "," );
   
   }else{
   echo "No way hose";
   }

?>

 

I dont get the error,. this is just strange.

 

Or to narrow it down even further,. I dont get the error when I take this out:

 

ON DUPLICATE KEY UPDATE ship_date = VALUES(ship_date),ship_via = VALUES(ship_via),job_name = VALUES(job_name),job_desc = VALUES(job_desc),finishing = VALUES(finishing),po_num = (po_num),cust_name = VALUES(cust_name)";

 

Is there an easier way to do this?

Ok,.. mark this one as solved. Turns out this was a mysql error and not the fgetcsv as the error message eluded to. This is the final code,.. please if any one see any problems with this let me know. And thanks for the help Barrand,.. I still owe you that beer..lol:

 

<?php 
$base = "/var/www/html/ups/";
$filename = $_FILES['file']['name'];

if (@is_uploaded_file($_FILES["file"]["tmp_name"])) { 
copy($_FILES["file"]["tmp_name"], "$base" . "$filename"); 
}
$filenameb= $base . $filename;
if (file_exists($filenameb)){
     echo "$filenameb</br>";
     $handle = fopen("$filenameb", "r") or die("no file");
     while (($data = fgetcsv($handle, 50000, "," )) !== FALSE ){
$import="INSERT INTO orders(`or_date`,`ship_date`,`ship_via`,`job_name`,`job_desc`,`finishing`,`or_num`,`po_num`,`cust_name`,`tracking`,`pro_date`) values('$data[0]','$data[4]','$data[5]','$data[6]','$data[7]','$data[8]','$data[1]','$data[2]','$data[3]','','')
ON DUPLICATE KEY UPDATE ship_date = values(ship_date), ship_via = values(ship_via), job_name = values(job_name), job_desc = values(job_desc), finishing = values(finishing), po_num = values(po_num), cust_name = values(cust_name), tracking = values(tracking), pro_date = values(pro_date)";
  mysql_query($import) or die("insert" . mysql_error());
}
fclose($handle);
     echo "Import Done";
   }else{
   echo "Import Unsuccessful";
   }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.