0
votes

Hi I badly need you help.

Error showing after importing .CSV file using mysql load data infile.

I have a form upload below which working fine

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

and a PHP upload script using load data infile.

require("../config/conn.php");


  if (is_uploaded_file($_FILES['my-file']['tmp_name']) && $_FILES['my-file']['error']==0) {
    $path = 'C:/xampp/htdocs/dom/test/uploads/' . $_FILES['my-file']['name'];
    if (!file_exists($path)) {
      if (move_uploaded_file($_FILES['my-file']['tmp_name'], $path)) {

        echo $mysql = "LOAD DATA LOCAL INFILE '".$_FILES['my-file']['name']."' 
                REPLACE INTO TABLE table 
                FIELDS 
                    TERMINATED BY ',' 
                LINES 
                    TERMINATED BY '\\n'
                IGNORE 1 LINES 
                (`col1`,`col2`,`col3`,`col4`,`col5`....)";

                $query = mysqli_query($link, $mysql) or die(mysqli_error($link));

if(!$query) 
{
    printf("Error message: %s\n", mysqli_error($link));     
}   




      } else {
        echo "The file was not uploaded successfully.";
      }
    } else {
      echo "File already exists. Please upload another file.";
    }
  } else {
    echo "The file was not uploaded successfully.";
    echo "(Error Code:" . $_FILES['my-file']['error'] . ")";
  }

Everything is fine, except the load data local infile cannot see the right path. please see the error: Can't find file 'logJan262013.CSV'. but the .csv file is uploaded successfully in folder 'uploads/'. Any help would be appreciated.

thanks alot!

3
Use a full path, not just the file name.Jeremy Harris
just because you moved the file to that uploads folder doesn't mean mysql will actually look for it there. You're also opening your server to a total remote compromise with this code (NEVER use user-provided ['name'] directly in filesystem operations).Marc B

3 Answers

1
votes

You're using the query:

$mysql = "LOAD DATA LOCAL INFILE '".$_FILES['my-file']['name']."'...

I do believe you are wishing to use the full path

$mysql = "LOAD DATA LOCAL INFILE '".$path."'...
0
votes

You do

 if (move_uploaded_file($_FILES['my-file']['tmp_name'], $path)) {

before

$query = mysqli_query($link, $mysql)

So, it is not surprising, that LOAD DATA doesn't find the file.

If you give LOAD DATA the proper $path, it should work as expected.

0
votes

Err you haven't told the LOAD DATA INFILE the path! Try telling it the path and all should be OK.