php mysql and LOAD DATA LOCAL INFILE forbidden
by Pedroski from LinuxQuestions.org on (#4YEDS)
I am reading a book by Kevin Yank: PHP & MySQL: Novice to Ninja. I am trying to load a .csv into a mysql table.
I installed apache2, php and mysql on this laptop.
In bash, logged in to mysql, this command will do the job:
Quote:
So now I'm trying to run this from php. This logs me in to mysql:
Code:<?php
try
{
$pdo = new PDO('mysql:host=localhost;dbname=allstudentsdb', 'myusername', 'mypassword');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::MYSQL_ATTR_LOCAL_INFILE);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$error = 'Unable to connect to the database server.';
include 'error.html.php';
exit();
}
?>I keep getting this error in the apache2 error.log
[Quote:
There must be a config file causing this trouble. Any clues where?
In php.ini I have:
Quote:
I was hoping this line in the /includes/studentdb.inc.php would do the trick, but I still keep getting the same "forbidden" error.
Quote:
Also, when I just set this in studentdb.inc.php, error.log complains: it wants 2 parameters, 1 given. What other parameter can it want??
The php file which should put the csv file in the table echoes out the mysql command to the output webpage. I can copy the command, use it in bash with mysql and I get exactly what I want. So there is nothing wrong with the command.
Any tips please??


I installed apache2, php and mysql on this laptop.
In bash, logged in to mysql, this command will do the job:
Quote:
| LOAD DATA LOCAL INFILE '/home/pedro/mycsvfiles/19BE1.csv' INTO TABLE 19BE1 CHARACTER SET utf8 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES; |
Code:<?php
try
{
$pdo = new PDO('mysql:host=localhost;dbname=allstudentsdb', 'myusername', 'mypassword');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::MYSQL_ATTR_LOCAL_INFILE);
$pdo->exec('SET NAMES "utf8"');
}
catch (PDOException $e)
{
$error = 'Unable to connect to the database server.';
include 'error.html.php';
exit();
}
?>I keep getting this error in the apache2 error.log
[Quote:
| Sun Jan 26 08:52:27.611033 2020] [php7:warn] [pid 22748] [client 127.0.0.1:44422] PHP Warning: PDO::exec(): LOAD DATA LOCAL INFILE forbidden in /var/www/html/importcsv/index.php on line 26, referer: http://localhost/importcsv/ |
In php.ini I have:
Quote:
| ; Allow accessing, from PHP's perspective, local files with LOAD DATA statements ; http://php.net/mysqli.allow_local_infile mysqli.allow_local_infile = On |
I was hoping this line in the /includes/studentdb.inc.php would do the trick, but I still keep getting the same "forbidden" error.
Quote:
| $pdo->setAttribute(PDO::MYSQL_ATTR_LOCAL_INFILE); |
The php file which should put the csv file in the table echoes out the mysql command to the output webpage. I can copy the command, use it in bash with mysql and I get exactly what I want. So there is nothing wrong with the command.
Any tips please??