Page 1 of 1

Uploading a file on my database--Error, query failed

Posted: Tue Dec 27, 2011 1:36 pm
by PHP_mySQL__Newbie
I am using the following code to upload files on a database using mySQL. The problem I am facing is that sometimes this codes works and sometimes it doesn't. I have tried with files of sizes 10K to 1MB. Behavior is uncertain. I receive the error 'Error, query failed'. Can someone please explain what is wrong with this code?


File: upload.php

Code: Select all

<?php require_once('../../Connections/myConn.php'); ?>

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

$query = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 

echo "<br>File $fileName uploaded<br>";

} 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload File</title>
</head>
<body>

<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr> 
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"> 
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

</body>
</html>
Code to create the upload table:

Code: Select all

CREATE TABLE upload (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
content LONGBLOB NOT NULL,
PRIMARY KEY(id)
);

Re: Uploading a file on my database--Error, query failed

Posted: Tue Dec 27, 2011 2:06 pm
by PHP_mySQL__Newbie
Sometimes I get this error for a csv file (size: 1MB):

Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in upload.php on line 33:
mysql_query($query) or die('Error, query failed');

Re: Uploading a file on my database--Error, query failed

Posted: Tue Dec 27, 2011 6:06 pm
by Christopher
Which line is 33 where you get the "MySQL server has gone away" error?

Re: Uploading a file on my database--Error, query failed

Posted: Tue Dec 27, 2011 11:29 pm
by PHP_mySQL__Newbie
At the bottom of the php code
line 33: mysql_query($query) or die('Error, query failed');

Re: Uploading a file on my database--Error, query failed

Posted: Wed Dec 28, 2011 2:59 am
by twinedev
Wow, never saw that one before, so I had to look it up:
http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

From what I am reading, the issue is the the mySQL server is closing your connection because it was open too long. One thing to try would be to do all teh actual file handling first, then actually open the connection to mySQL so that all happens after the long wait on large files.

-Greg

Re: Uploading a file on my database--Error, query failed

Posted: Wed Dec 28, 2011 7:40 pm
by Christopher
Interesting. Open a connection to MySQL, load a large file into memory, connection times out.