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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHP_mySQL__Newbie
Forum Commoner
Posts: 29
Joined: Fri Aug 12, 2011 5:40 pm

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

Post 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)
);
User avatar
PHP_mySQL__Newbie
Forum Commoner
Posts: 29
Joined: Fri Aug 12, 2011 5:40 pm

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

Post 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');
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

Which line is 33 where you get the "MySQL server has gone away" error?
(#10850)
User avatar
PHP_mySQL__Newbie
Forum Commoner
Posts: 29
Joined: Fri Aug 12, 2011 5:40 pm

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

Post by PHP_mySQL__Newbie »

At the bottom of the php code
line 33: mysql_query($query) or die('Error, query failed');
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

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

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post by Christopher »

Interesting. Open a connection to MySQL, load a large file into memory, connection times out.
(#10850)
Post Reply