PHP Joomla 2.5 MySQL Load Data Infile - Permission denied - php

I'm trying to load a CSV file into a table. I'm getting a permission denied error:
Error importing CSV File: (1045) Access denied for user 'username'#'localhost'
(using password: YES)
SQL=LOAD DATA INFILE '//homeX/username/public_html/activator/uploadedCSV/20180603064917_csvtoimport.csv'
IGNORE INTO TABLE `csv_import_temp`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
( contact_lname, contact_email ) ;
The csv file exists, and has permissions set to 0644.
The SQL statement is:
LOAD DATA INFILE '//homeX/username/public_html/activator/uploadedCSV/20180603064917_csvtoimport.csv'
IGNORE
INTO TABLE `csv_import_temp`
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\r\n'
IGNORE 1 ROWS
(
contact_lname,
contact_email
);
I've tried using it with both a leading single slash and the double slash (as above) in the filename. I've also tried running it in phpAdmin and get the same error message.
The site is hosted on a Hostgator shared account.
I've added code to verify that the file exists, and has the right permissions, and it says it does. But MySQL doesn't seem to be able to access the file.
Yes, I know that Joomla 2.5 is end of life, but for many reasons, this site can't be upgraded, and is only used for admin functions.
Any suggestions would be welcome as the alternative is to read the file with PHP and insert each row which would be much slower.
The first 4 lines of the CSV:
"name","email"
"John Smith","xxx1#yyy.edu"
"John Doe","xxx2#yyy.edu"
"Jane Doe","xxx3#yyy.edu"

This error is usually due to you not including the LOCAL keyword. Try:
LOAD DATA LOCAL INFILE '//homeX/username/public_html/activator/uploadedCSV/20180603064917_csvtoimport.csv'
IGNORE
INTO TABLE `csv_import_temp`
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\'
LINES TERMINATED BY ''
IGNORE 1 ROWS
(
contact_email,
contact_email
);
The permission error is misleading, it relates to trying to the mysql system directories rather than the file you are trying to read.

Related

Load data local infile stop working and it doen't give any error

I have a PHP script where I upload a CSV file and this one update/insert rows into mysql database, all is on ubuntu 18.04 server and this PHP script had many years working without problems, the server has more than 1 year working, suddenly a couple of weeks ago, with any change on server or software stop working.
The weird thing is it is not generating any error, no logs error, nothing, so i can't debug.
I read a lot this few days ago without any lucky, so i am not sure what to do.
On my tests and testing similar problems, i found this:
If i run the "Load data local infile" directly on the server it works
The file is uploading fine, i can give 777 permision and nothing changes
infile is enabled on mysql
apparmor is configured
user who connect to database has FILE on privileges
Here is the used code:
LOAD DATA LOCAL INFILE '/path/to/temp/file' INTO TABLE pagos_nuevos CHARACTER SET latin1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES
(cuenta, #pago, #fpago) SET pago = CAST(REPLACE(REPLACE(#pago,',',''),'$','') AS DECIMAL(10,2)), fpago = STR_TO_DATE(#fpago, '%d/%m/%Y')
As I said at the begining, this is no new server, this is no new code, has many years working, when debug I only received this on screen:
Query: LOAD DATA LOCAL INFILE '/path/to/temp/file' INTO TABLE pagos_nuevos CHARACTER SET latin1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES
(cuenta, #pago, #fpago) SET pago = CAST(REPLACE(REPLACE(#pago,',',''),'$','') AS DECIMAL(10,2)), fpago = STR_TO_DATE(#fpago, '%d/%m/%Y') failed
0:
Where is "0:" it must to be the error code and after ":" the description of the error.
If I remove LOCAL i get this error:
Query: LOAD DATA LOCAL INFILE '/path/to/temp/file' INTO TABLE pagos_nuevos CHARACTER SET latin1 FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES
(cuenta, #pago, #fpago) SET pago = CAST(REPLACE(REPLACE(#pago,',',''),'$','') AS DECIMAL(10,2)), fpago = STR_TO_DATE(#fpago, '%d/%m/%Y')
not found (Errcode: 13 - Permission denied)
29: File '/var/www/html/testing/unicomer/pagos/datos/pagos/pagos al 29.csv' not found (Errcode: 13 - Permission denied)
I did a test with a user with ALL THE PRIVILEGES and do not work, so I don't know if this is a mysql issue or server issue.
Thank you very much for your help.

Yii2 LOAD DATA LOCAL INFILE

I know there are other solutions to this problem, but it did not work for me.
I am trying to execute a LOAD DATA LOCAL INFILE statement in a MySQL database via a Yii2 website. I have got this working in the past, so I am not sure why it doesn't work anymore.
I get the following error:
SQLSTATE[42000]: Syntax error or access violation: 1148 The used
command is not allowed with this MySQL version
I do have the following setting in my db.php config file.
'attributes' => [ PDO::MYSQL_ATTR_LOCAL_INFILE => true ],
I also tried disabling secure-file-priv to no avail.
EDIT
I tried running the following query from Workbench and it throws an error via Workbench as well. However, it works on MySQL 5.7. I am currently using 8.0
LOAD DATA LOCAL INFILE 'D:/Temp/6_attlog.dat' IGNORE
INTO TABLE att_log
FIELDS TERMINATED BY '\t' ENCLOSED BY '''' LINES TERMINATED BY '\r\n' IGNORE 0 LINES
(#id, `date_time`, `dev_id`, `mode`, `work_code`, `work_code1`) SET `id` = TRIM(#id)
EDIT 2
I got it to work with "LOAD DATA INFILE"...
...but it does not work with "LOAD DATA LOCAL INFILE"
Here's the code I was using...
$inFile = str_replace('\\', '/', realpath('uploads/'.$model->file->name));
Yii::$app->db->createCommand('
LOAD DATA LOCAL
INFILE \''.$inFile.'\' IGNORE
INTO TABLE att_log
FIELDS TERMINATED BY \'\\t\'
ENCLOSED BY \'\'\'\'
LINES TERMINATED BY \'\r\n\'
IGNORE 0 LINES
(#id, `date_time`, `dev_id`, `mode`, `work_code`, `work_code1`)
SET `id` = TRIM(#id)
')->execute();
unlink($inFile);
According to MySQL documentation, server needs to be configured to allow LOCAL keyword:
LOCAL works only if your server and your client both have been configured to permit it. For example, if mysqld was started with the local_infile system variable disabled, LOCAL does not work.
https://dev.mysql.com/doc/refman/8.0/en/load-data.html
Are you sure that your server is correctly configured to allow this keyword?
Just a suggestion
in PHP you can use single and double quote so you could build you command in a simple an more clear way eg:
$inFile = str_replace('\\', '/', realpath('uploads/'.$model->file->name));
Yii::$app->db->createCommand("
LOAD DATA LOCAL
INFILE '".$inFile."' IGNORE
INTO TABLE att_log
FIELDS TERMINATED BY '\t'
ENCLOSED BY '\'
LINES TERMINATED BY '\r\n\'
IGNORE 0 LINES
(#id, `date_time`, `dev_id`, `mode`, `work_code`, `work_code1`)
SET `id` = TRIM(#id)
")->execute();
unlink($inFile);

Yii: Import CSV Data and save to database

I want to save my CSV File data to my database and using "Load Data Infile" as-
$sql="LOAD DATA INFILE '".$tempLoc."'
INTO TABLE `tbl_csv`
FIELDS
TERMINATED BY ','
ENCLOSED BY '\"'
LINES
TERMINATED BY '\r\n'
(`mobile`, `name`, `email`) ";
I keep getting syntax errors for this query statement.
Error: CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 13 Can't get stat of '/tmp/phpikiYm6' (Errcode: 2). //tmp/phpikiYm6 is the file name
Sorry but I have no much reputations to comment on your question, however I cant see that you give the file extension to the query, have you tried:
$sql="LOAD DATA INFILE '".$tempLoc.".csv'
INTO TABLE tbl_csv
FIELDS
TERMINATED BY ','
ENCLOSED BY '\"'
LINES
TERMINATED BY '\r\n'
(mobile, name, email) ";
Follow given checks to fix this problem :
perror 13
OS error code 13: Permission denied
Check these variables (using SHOW VARIABLES):
have_csv = YES
local_infile = ON
If those are correct, then...
The .csv file, I think, needs to be readable and locatable by the "mysql" user. Note that both "r" and "x" is needed on directories to let a user 'read' and 'search' the directory.
Suggest you change permissions on the file and directories of this part to 777:

Uploaded tmp file not found in query

I'm fairly new to php and CakePHP, and I'm trying to execute the following code after uploading a text file (2kb):
$filefullname = $this->request->data['File']['file']['tmp_name'];
debug($filefullname);
move_uploaded_file($filefullname, WWW_ROOT.'tmp.txt');
$query = 'LOAD DATA LOW_PRIORITY INFILE "'.WWW_ROOT.'tmp.txt'.'" INTO TABLE agencies FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES';
debug($query);
$this->Agency->query($query);
Though the file can be found, I get the following output:
'C:\Windows\Temp\phpB413.tmp'
'LOAD DATA LOW_PRIORITY INFILE "C:\Data\myphpapplication\app\webroot\tmp.txt" INTO TABLE agencies FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES'
Error: SQLSTATE[HY000]: General error: 29 File 'C:\Data\myphpapplication\app\webroot\tmp.txt' not found (Errcode: 13)
SQL Query: LOAD DATA LOW_PRIORITY INFILE "C:\Data\myphpapplication\app\webroot\tmp.txt" INTO TABLE agencies FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r" IGNORE 1 LINES
How can I fix this?
Most likely the file is there, but MySQL doesn't allow you to read files from that location. MySQL is fairly restrictive from where it reads LOAD DATA ... INFILE, with good reasons. You may need to move your file first to a path that MySQL can read from, or change the settings for your MySQL server.
From the MySQL 5.1 manual:
Note that, in the non-LOCAL case, these rules mean that a file named
as ./myfile.txt is read from the server's data directory, whereas the
file named as myfile.txt is read from the database directory of the
default database. [...]
And:
Windows path names are specified using forward slashes rather than
backslashes. If you do use backslashes, you must double them.
#JvO is guiding you down the right path. I think you're missing a couple of important elements, one of which is moving the uploaded file from its initial temp directory on the server to a place where MySQL can officially read it (it won't read it from a temp directory for security reasons).
Here's an example from a project I did, which uploaded a CSV file and then MOVED it to a readable directory. I first uploaded it with the move_uploaded_file function, and then created a variable describing the uploaded path of the new file for use in a LOAD DATA query.
move_uploaded_file($_FILES["fCSV"]["tmp_name"],"user_uploads/SG".$dapID ."/". $_FILES["fCSV"]["name"]);
$csvfile = "user_uploads/SG".$dapID ."/". $_FILES["fCSV"]["name"];
In your LOAD DATA query, you're missing the "LOCAL" keyword, which tells MySQL to read the file from a specified directory, otherwise it's looking for it in the database directory (the use of "Field1" etc. below is for demo purposes only and you should have your real table field names in there):
$sqlLoad = "LOAD DATA LOCAL INFILE '".$csvfile."' INTO TABLE my_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (Field1,Field2,Field3,Field4) SET ID =".$id
Lastly, you really need to read through this page in the MySQL manual. It covers many of the issues you're dealing with along with providing examples:
MySQL LOAD DATA INFILE Syntax

Problems in creating output file of my query result

Following is my sql query which worked fine when i ran it from phpmyadmin but when i ran the same query through PHP then the query didnot worked and gave me following error. Kindly let me know how can I run this query from my php file so it will create the output file in the specified folder. Thanks,
QUERY
SELECT * FROM lahore_student INTO OUTFILE 'b://uploaded//data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
LINES TERMINATED BY '\n'
Output: It creates file to my b drive in FOLDER named as Uploaded
ERROR:
File 'b:uploadeddata.csv' already exists
NOTE: The uploaded folder was empty
It looks you are trying to run this on a windows system, which requires special treatment of directory separators.
Try:
SELECT * FROM lahore_student INTO OUTFILE 'b:\\uploaded\\data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
LINES TERMINATED BY '\n'
Or:
SELECT * FROM lahore_student INTO OUTFILE 'b:/uploaded/data.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '".'"'."'
LINES TERMINATED BY '\n'
Also note that this will not overwrite an existing file (which is the error you are getting). So you need to make sure that any file with that name is deleted first.
See the The “\” path name separator character section on this page in the MYSQL docs:
http://dev.mysql.com/doc/refman/5.0/en/limits-windows.html

Categories