Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LOAD DATA LOCAL INFILE forbidden in... PHP

Tags:

php

mysql

I am trying to use LOAD DATA INFILE to insert some records into a table. Unfortunately, it's not working.

Here are some details

If I use this instruction:

LOAD DATA INFILE 'file.txt' INTO TABLE table_ex FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1, field2, field3, field4); 

It works using the MySQL client program and a PHP application. In this way it will look for the file in the Data Directory of my MySQL installation.

Now if I try to execute the instructions using the LOCAL option, it only works if I use the mysql client, but not from PHP:

LOAD DATA LOCAL INFILE 'path/to/file/file.txt' INTO TABLE table_ex FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (field1, field2, field3, field4); 

Again.. it works with MySQL client but not from the PHP application... I get this error:

LOAD DATA LOCAL INFILE forbidden in /path/to/my/application 

I read that the problem is related to the compilation of PHP and using mysqlnd. I am using PHP 5.3.8 and MySQL 5.5.15, but I haven't found a solution.

Additional information: until now the only help I've found was an open PHP bug:

like image 872
Richard Pérez Avatar asked Oct 03 '11 16:10

Richard Pérez


People also ask

How do I enable local data Infile?

To disable or enable it explicitly, use the --local-infile=0 or --local-infile[=1] option. For the mysqlimport client, local data loading is not used by default. To disable or enable it explicitly, use the --local=0 or --local[=1] option.

What is load data local infile?

The LOAD DATA INFILE statement reads rows from a text file into a table at a very high speed. If the LOCAL keyword is specified, the file is read from the client host. If LOCAL is not specified, the file must be located on the server. ( LOCAL is available in MySQL 3.22.

How does load data local infile work?

In contrast, when you execute the LOAD DATA LOCAL INFILE statement, the client attempts to read the input file from its file system, and it sends the contents of the input file to the MariaDB Server. This allows you to load files from the client's local file system into the database.

How do I connect to the local Infile system variable?

To cause the server to permit access, set the local_infile variable with SET GLOBAL local_infile = 1; or check it with SHOW GLOBAL VARIABLES LIKE 'local_infile'; . Alternatively, edit mysql's config to include local_infile=1 .


2 Answers

Check docs http://php.net/manual/en/ref.pdo-mysql.php.

Basically you need:

PDO::MYSQL_ATTR_LOCAL_INFILE => true 

Set at instantiation.

Example:

    $conn = new \PDO("mysql:host=$server;dbname=$database;", "$user", "$password", array(         PDO::MYSQL_ATTR_LOCAL_INFILE => true,     )); 
like image 60
Nomas Prime Avatar answered Sep 24 '22 23:09

Nomas Prime


had this problem today and solved it by setting the following in php.ini

mysqli.allow_local_infile = On 
like image 34
Sebastian Avatar answered Sep 21 '22 23:09

Sebastian