Uploaded tmp file not found in query - php

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

Related

How to upload a .csv file that contains line breaks to a MySQL database

We are writing some php to move data from a Microsoft SQL database to a MySQL database. The Microsoft data contains user input text, with line breaks and all sorts of wacky stuff
The process we've been working with is as follows:
select from a Microsoft SQL database into php array (works fine)
processes this into a .csv by looping the fputcsv() function:
fputcsv($delimiter = ',', $enclosure = '"', $escape_char = "\\");
(the .csv file looks good when I open it with google sheets)
upload the .csv file to the MySQL server with the following code:
LOAD DATA LOCAL INFILE 'c:/filename.csv'
INTO TABLE tablename
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS; ```
The problem is that when step 3 is executed, the resulting MySQL table is all jumbled up because it appears to get confused with the line breaks in the data, thinking it's a new line in the table.
I noticed the MySQL LOAD DATA command doesn't take the escape character as an argument, but I can't seem to find a way to give it to it. Is this the issue? How can we make this work?

PHP Joomla 2.5 MySQL Load Data Infile - Permission denied

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.

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:

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

Upload a csv and import it into the mysql database

I am writing the following script up import a csv file into a mysql database.
The user has to log in first and their security rights to upload are checked by another file, if this evaluates to true, the file can then be uploaded by the file up-loader which will only allow csv's to be uploaded and then the following part imports the file.
Am I using the correct code for this below ?, also if the csv layout is wrong is it possible to refuse the import ?, im just concerned that this could go badly wrong if the csv is formatted correctly. This feature has been requested as a requirement for this project so I am just trying to make it as idiot proof as possible for them.
<?php
$uploadedcsv = './uploads/'.$filename.'';
$sql = 'LOAD DATA LOCAL INFILE "'.$uploadedcsv.'" INTO TABLE '.$table.' FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" IGNORE 1 LINES' or die(mysql_error());
?>
Unless you are 100% rely on the user and it seems that you are not,
it will be good not to blindly import uploaded file, but first to check if it's correct.
To do it, you'll need to open and to read the file, e.g. with fgetcsv and to check the data consistency line-by-line.
You can find a lot of examples on the web.
Here are just some:
http://www.damnsemicolon.com/php/php-upload-csv-mysql
http://www.programmingfacts.com/import-csvexcel-data-mysql-database/
It can be done via MySQL, but by default LOAD DATA INFILE expects a different format than CSV. From the documentation:
If you specify no FIELDS or LINES clause, the defaults are the same as if you had written this:
FIELDS TERMINATED BY '\t' ENCLOSED BY '' ESCAPED BY '\\'
LINES TERMINATED BY '\n' STARTING BY ''
The documentation notes, for when dealing with CSV files:
LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES;
Which is an example of dealing with comma separated values enclosed by "" and line-separated by \r\n
To use this to import a file, make sure your statement matches the CSV format of your upload files and you can import via this manner.

Categories