2

I need a simple way to load a csv file from my computer to the database (which is on a remote server). I'm trying to load the file using a php page that executes this code:

    $file = $_FILES['csv']['tmp_name']; 
    $handle = fopen($file,"r"); 
    $name = $_FILES['csv']['name'];

   $import = "LOAD DATA LOCAL INFILE '" . $name .
                "' INTO TABLE temporal_load
                  FIELDS TERMINATED BY ','  
                  optionally ENCLOSED BY '\"' 
                  LINES TERMINATED BY '\\n' 
                  IGNORE 1 LINES
                  (num,ticker,company,sector,industry) ";

mysql_query($import) or die(mysql_error());

But when I select a file on the php page and try to load it, shows the error message:

File 'test11-14-15.csv' not found (Errcode: 2)

I already reviewed that the variable mysql.allow_local_infile is ON and the database connection was made this way:

mysql_connect(HOST,USER,PASSWORD,false,128);

The file is not on the remote server, the file is on my computer, but it should work anyway because I'm using LOCAL.

What am I doing wrong???

4
  • LOCAL must be LOCAL relate to MySQL server, with file path on the server
    – a_vlad
    Nov 23, 2015 at 19:41
  • where is the php server in relation to the file? Are you uploading the file to the php server which is actually the client executing LOAD DATA LOCAL INFILE? Nov 23, 2015 at 20:05
  • The file is on the client side, the php server is on a remote server. I want to load the file from client's computer to the php server (which also has the Mysql database) Nov 24, 2015 at 20:45
  • Don't use mysql_*, use mysqli_*.
    – Rick James
    Dec 2, 2015 at 19:19

2 Answers 2

0

Your problem is on using the variable $name on your LOAD DATA LOCAL INFILE statement, you should use the variable $file in your statement..

Your statement should change to;

$import = "LOAD DATA LOCAL INFILE '" . $file. "' INTO TABLE temporal_load FIELDS TERMINATED BY ',' optionally ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (num,ticker,company,sector,industry) ";

mysql_query($import) or die(mysql_error());
-1

You need to use $_FILES['csv']['tmp_name'] instead of $name in your import query.

So your php function might look like like:

$filename = $_FILES['csv']['tmp_name'];

$import = "LOAD DATA LOCAL INFILE '" . $filename . "' INTO TABLE temporal_load FIELDS TERMINATED BY ',' optionally ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 LINES (num,ticker,company,sector,industry) ";

mysql_query($import) or die(mysql_error());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.