phphopzter
phphopzter

Reputation: 121

Mysql load data infile wrong path

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!

Upvotes: 0

Views: 1805

Answers (3)

Captain Payalytic
Captain Payalytic

Reputation: 1071

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

Upvotes: 0

Olaf Dietsche
Olaf Dietsche

Reputation: 74048

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.

Upvotes: 0

UnholyRanger
UnholyRanger

Reputation: 1971

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."'...

Upvotes: 1

Related Questions