I want to load a huge amount of data from a CSV file. I wrote this program to do it but when I execute the program it shows me an error. The error is:
file not found
If I echo the query then the file path that is shown is different than the mysql search for the file.
$fileName = $_FILES["file"] ["tmp_name"];
// Creating a temporary table to hold csv file data....
$tmp_tbl = "CREATE TABLE track LIKE trackingsite";
mysqli_query($this->conn, $tmp_tbl) or die( "Temporary table creation failed: " . mysqli_error($this->conn) );
$file_load = "LOAD DATA INFILE '$fileName' INTO TABLE track FIELDS TERMINATED BY ','OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' ";
echo $file_load;
echo '<br>';
echo '<br>';
mysqli_query($this->conn, $file_load) or die( "Can not insert data: " . mysqli_error($this->conn) );
I think you should use absoulute directly path to load the file.
Like 'for exmaple "C://xammp/htdoc" in $fileName
This question has been asked but no answers yet...
Have a csv upload form to load into an existing MySQL table for use on a Drupal page, hence everything being in one php. This is what I have (thanks coyotelab):
$mysqli = new mysqli($server, $user, $password, $database);
if ($mysqli->connect_error) {
die("Failed to connect: " . $mysqli->connect_error);
}
if (isset($_POST['submit'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
echo "<h2>" . "File ". $_FILES['filename']['name'] ." uploaded successfully." . "</h2>";
}
$handle = fopen($_FILES['filename']['tmp_name'], "r");
if ($handle !== FALSE) {
$import="LOAD DATA LOCAL INFILE '" . $_FILES['filename']['tmp_name'] . "' INTO TABLE Demo2 CHARACTER SET 'utf8' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' IGNORE 1 LINES;";
mysql_query($import) or die('<p style="color:red;">Database import unsuccessful: ' . mysql_error() . '</p>');
}
fclose($handle);
print "<p style='color:blue;'>Import done</p>";
echo "<p style='position: relative; top: 10px;'><strong>Displaying contents:</strong></p>";
readfile($_FILES['filename']['tmp_name']);
unset($_POST);
} else {
print "<p>Import new csv</p>\n";
print "<form enctype='multipart/form-data' action='?q=drupal-relative-path-to/upload.php' method='post'>\n";
print "<input size='50' type='file' name='filename'><br />\n";
print "<input type='submit' name='submit' value='Import'></form>";
At first, the form was test locally and granting all permissions to localhost worked and data imported successfully. But on a Drupal server, there will be no change to localhost permissions. Other forms to insert SQL data have worked fine from Drupal pages. Only happens with the upload file function.
So far the form shows up on Drupal, but when uploading and submitting a CSV, I get the same error as testing on localhost without permissions. Error is "No database selected"
My thought was to change the location where uploaded temp files live, though haven't figured that out or might not be possible on the Drupal server. Tried adding a second db connect line, but that doesn't work. Must have something to do with the temp directory.
This form will be on a private network.
If error is "No database connection" then your code fails on very first code row displayed here. Something is wrong with your database user account information (or account it self). Maybe host isn't "localhost" but something else?
If Drupal is working well check out it's connection. Check file /sites/default/settings.php. There is stored information about database connection so you can copy your account data from there.
BTW, instead of old school fopen on PHP you have easier to use file_get_contents() function.
So I'm getting a Pdf file send to my webservice for me to upload,
it's send to me as a base64Binary type variable,
and uploaded using below script :
function SaveEIM($parameters)
{
mysql_query("INSERT INTO tablename (Filename, FileSize, File)
VALUES ( '" . $parameters['Filename'] . "', '". $parameters['FileSize'] . "', '" . $parameters['File'] . "' ) ") or die (mysql_error());
}
Now everything is getting uploaded, only the weird thing is, for instance if I upload an 370KB file, it's 490KB in the Database....
I don't know how this happens, and if this is normal?
Now to save it in my fileserver :
$query = mysql_query("SELECT File FROM tablename WHERE ID = '1' ") or die (mysql_error());
$fetch = mysql_fetch_array($query);
$data = $fetch[0];
file_put_contents("user_123.pdf", $data);
When I open this file that is getting saved now in my fileserver, I get this error : "user_123.pdf cannot be opened because this filetype is not supported or because it is damaged."
I'm fairly new to Blob types, and I know that storing files on your server is sometimes better, but I have to use Blob times, since I was asked to do so.
If anyone could help me out here, it would be greatly appreciated.
Solved it, had to use base64_decode($data) on my blobtype.
My glob code finds the file and attempts to insert it into the table but fails. I couldn't find anyone that is asking the same question. I will have many files that need to be inserted into the same table. It is a coma delimited text file.
Updated code*
The following code works as long as I rem the load data line. When I use the load data command manually, it works. I have not found a post example that has solved the problem "Why does the load data command not work?"
$con = new mysqli('localhost', 'username', 'password', 'schaadan_hash');
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Connected. ";
echo "<br />";
}
$upload = "LOAD DATA LOCAL INFILE 'ntlmhash2_1.txt' INTO TABLE ntlm2 FIELDS TERMINATED BY ',' ENCLOSED BY '' ESCAPED BY '\\' LINES TERMINATED BY '\n'(`clear` , `hash`);";
mysqli_query($con,$upload);
?>
I've got this error:
Error message is :: "Error occured during query execution: (LOAD DATA LOCAL INFILE '/tmp/3sch.jofcial.txtgymCN5' REPLACE INTO TABLE `prod_sch` FIELDS TERMINATED BY ',' IGNORE 1 LINES (No_ ,Model_Code,Model_Name ,Lot_No_ ,Start_Seq_No_ ,Quantity ,Lot_Quantity ,Line_Code ,Line_Name ,#var10, Shift_No1,#var12,Shift_No2,#var14,Production_Mngm_Type,Model_Group,Model_Category,Actual_Results,MPS_Mngm_Code, Production_Mngm_Code,Comment) SET Production_Start_Date=STR_TO_DATE(#var10,'%m/%d/%Y'), Production_Finish_Date=STR_TO_DATE(#var12,'%m/%d/%Y'), Due_date=STR_TO_DATE(#var14,'%m/%d/%Y')): The used command is not allowed with this MySQL version";
After try this syntax:
$temp_file = tempnam(sys_get_temp_dir(), '3sch.jofcial.txt'); // make temporary file name
$fp = fopen($temp_file, "wb"); // open temprary file
if (!$fp) die(_ERROR14);
fwrite($fp, $httpfile); // copy to temporary file from schedule file
fclose($fp);
chmod($temp_file, 0644); // file mode change for read only
$sql="TRUNCATE TABLE `prod_sch`";
$res=mysql_query($sql) ;//or _doError(_ERROR30 . ' (<small>' . htmlspecialchars($sql) . '</small>): ' . mysql_error() ); // submit SQL to MySQL $
$sql="LOAD DATA LOCAL INFILE '".$temp_file."' REPLACE INTO TABLE `prod_sch` FIELDS TERMINATED BY ',' IGNORE 1 LINES "; // FIELDS ENCLOSED BY '\"'$
$sql.="(No_ ,Model_Code,Model_Name ,Lot_No_ ,Start_Seq_No_ ,Quantity ,Lot_Quantity ,Line_Code ,Line_Name ,#var10, ";
$sql.="Shift_No1,#var12,Shift_No2,#var14,Production_Mngm_Type,Model_Group,Model_Category,Actual_Results,MPS_Mngm_Code, ";
$sql.="Production_Mngm_Code,Comment) ";
$sql.="SET Production_Start_Date=STR_TO_DATE(#var10,'%m/%d/%Y'), ";
$sql.="Production_Finish_Date=STR_TO_DATE(#var12,'%m/%d/%Y'), ";
$sql.="Due_date=STR_TO_DATE(#var14,'%m/%d/%Y')";
$res=mysql_query($sql) or _doError(_ERROR30 . ' (<small>' . htmlspecialchars($sql) . '</small>): ' . mysql_error() ); // submit SQL to MySQL and$
unlink($temp_file);
This error happen after restore the backup file into new server. From the error message I can see that the temporary file already created.
EDIT
I have try:
mysql --load-infile -u root -p pwd DBname
then try to put the query above inside >mysql, this query can work.
But, why if i using php it doesn't?
try to edit /etc/mysql/my.cnf:
add local-infile = 1
still can't work.
this the php page
Problem solved
Just change:
$dbc=mysql_connect(_SRV, _ACCID, _PWD) or die(_ERROR15.": ".mysql_error());
Into :
$dbc=mysql_connect(_SRV, _ACCID, _PWD,false,128) or die(_ERROR15.": ".mysql_error());
Tadaaa...all error is gone!!
From here:
If LOAD DATA LOCAL is disabled, either in the server or the client, a client that attempts to issue such a statement receives the following error message:
ERROR 1148: The used command is not allowed with this MySQL version