简体   繁体   中英

php LOAD DATA LOCAL INFILE csv to mysql

I've been trying this for a while and investigating as many avenues as I could before asking my first questions here.

I have a very simple csv file. One column, data separated by linux line feeds. I want to import it into the 2nd column (passcode) of a table (codes). The first column is a primary key auto incrementing.

Here's the simple csv data (linux line feeds - \\n)

A12345678911
A12345678912
A12345678913
A12345678914

Here's the form to gather the file.

<form enctype="multipart/form-data" action="upload_webcodes.php" method="post">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
        <input type="hidden" name="success" value="success" />
    Send this file: <input name="webcodeupload" type="file" />
    <input type="submit" value="Send File" />
</form>  

Here's the php which is attempting to run the query to load the tmp file into the database.

<?php

function dbconnect(){
$db = mysql_connect('localhost', 'root', 'root');
mysql_select_db("mydatabase", $db);
return $db;
}

if ($_FILES['upload']['type'] === 'text/csv') { 
    $database = dbconnect();
    $getfile = $_FILES['upload']['tmp_name'];   
    $sql = " LOAD DATA LOCAL INFILE $getfile INTO TABLE codes (`passcode`) LINES TERMINATED BY '\n' ";
    mysql_query($sql);   
}
?>

I can't get it to work. I'm trying this locally with MAMP. The /tmp/php folder Date Modified gets updated but I can't see a file there. I've even tried larger files since my test file is so simple. I have printed out the $_FILES array after submitting the form.

Array
(
    [webcodeupload] => Array
        (
            [name] => Aimport.csv
            [type] => text/csv
            [tmp_name] => /Applications/MAMP/tmp/php/phpsp9ezx
            [error] => 0
            [size] => 52
        )

)

It seems that the expected results are there? I've tried the query in phpmyAdmin and it works if I remove the LINES TERMINATED BY clause. I suspect that's a phpmyAdmin thing, and i've also tried this code with and without that clause in the query.

Additionally there are a few "undefined index" errors when loading the page. I sort of understand why, and have read threads here indicating ways to mask the errors, but I'd be interested in knowing how to avoid them rather than hide them.

Any input would really be appreciated.

Thanks.

First, You've analyzed correctly, the file should be in /tmp/php . So, you should check

  1. permission in /tmp/php folder.
  2. in order to MySQL to be able to read files from the /tmp/php directory, in linux, don't forget to set apparmor configuration in /etc/apparmor.d/usr.sbin.mysqld by adding this line /tmp/php/** rwk,

Change your condition to:

if ($_FILES['webcodeupload']['type'] == 'text/csv') {
    // what you have now
}

The dump you provided shows the form input field is "webcodeupload", but your PHP is checking for "upload".

The reason the file isn't there when your script ends is well, that's how php works. You must call the move_uploaded_file function to actually move the uploaded file somewhere, otherwise PHP deletes it when the script ends (to prevent resource consumption attacks).

Note that there may be other problems with what's in your if statement, but this'll get you closer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM