Search code examples
phpmysqlload-data-infile

The used command is not allowed with this MySQL version


Alright, so after a week of trying all the different ideas answers I have found with no success, I am going to ask. This is for the LOAD DATA LOCAL INFILE, for MySQL. There have been many posts regarding this, but I am still unable to get it work from the web browser, but am able to run it from the mysql command prompt on the server, with the same user and pass connecting to the same database. What I have tried so far

  1. MySQL version 5.5
  2. Ubuntu 12.04
  3. PHP version 5.3

In my.cnf local-infile=1 in mysqld,mysql,mysql-safe loose-local-infile=1 client

Restarted MySQL Server. At this point I was then able to run the query from the command prompt, and previously had not. I have given the directory in which the files are being pulled from 777 access. I have confirmed the php.ini has the local file parameter enabled. I have updated apparmor. Actual Query:

LOAD DATA LOCAL INFILE '/var/www/ui/uploads/r_import.csv' INTO TABLE r_data FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 ROWS (first_name,last_name,apt,user_id)

Above query works from the mysql command with no special arguments in the connection to the server.

If anyone has anymore ideas on this, I would be happy to try anything.... Thanks in advance.

<?php
include 'includes/header.php';
if($_FILES['file']['type'] != "application/vnd.ms-excel"){
   die("This is not a CSV file.");
}
elseif(is_uploaded_file($_FILES['file']['tmp_name'])){
    $filename = $_FILES['file']['tmp_name'];
    $name = $_FILES['file']['name'];
    copy( $filename, 'uploads/'.$name ) or die( "Could not copy file!");
    $file_to_import = '/var/www/ui/uploads/'.$name;
   $query = 'LOAD DATA LOCAL INFILE  \''.$file_to_import.'\' INTO TABLE r_data FIELDS TERMINATED BY  \',\' ENCLOSED BY  \'"\' LINES TERMINATED BY  \'\n\' IGNORE 1 ROWS (first_name,last_name,apt,user_id)';
   echo $query;
   $result = mysqli_query($link,$query) or die(mysqli_error($link));
}
else{
   die("You shouldn't be here");
}
?>

Solution

  • $link = mysqli_init();
    mysqli_options($link, MYSQLI_OPT_LOCAL_INFILE, true);
    mysqli_real_connect($link, 'localhost', $username, $password, $database);
    

    The connection string to the database is what worked for me. I found it in the last comment in the link given by developerwjk