Page 1 of 1

problem saving image in mySql, not mysql max packet size

Posted: Sat Dec 20, 2003 4:03 pm
by Hurklefish
Howdy.

I'm going nuts here.

I have a script that lets users upload images into a database. On larger images, it's just not working. I get the error message 'mysql server has gone away'.

A few things.. first of all, I'm very familiar with all the reasons folks say this is a bad idea.. I know that many reccomend saving all the image properties in a db, sticking the file itself in an actual directory, and using the db to point to it.. However, for various reasons, I want it to work this way, actually storing the image in the db.

Second, I got this working once before, I built a script that would let folks upload all kinds of files, not just images, and it worked, evern if the files were 40mb or bigger. Ran stable and uploaded thousands of files over the course of months without erros. I don't have that code handy, but I know it worked before, and I can't figure out what the issue is.

Third, I know that one of the most common culprits for this problem is that mysql has a max packet size default setting of 1mb. I've bumped this up to 16mb, so that shouldn't be an issue..

Also, the image data is being stored as a long blob, which theororetically (sp?) should be able to handle 2 to 4 gig, not that that's gonna happen..

When I upload images less than a meg, it all works peachy. Uploading a 1.4 meg jpeg image is what makes it break.

The upload form is passing everything correctly. I've checked the size of the variable holding the image data, and it's exactly the right size, but the insert just doesn't happen.

I've searched thru google, and a bunch of the usual forums, and I'm coming up goose eggs.

I'm running php, mysql, and apache on a windoze xp pro box (i know.. I'll be going all LAMP asap..)

Any insights or suggestions would be great.

Just to recap, it's the insert that fails.. everything else works great, and the data for the insert is there.. mysql just chokes on it.

Code below.. Thanks! (note.. where it says my_db, my_password, etc., the actual stuff is in the real code..)

Code: Select all

ob_start();

	session_start();



	

	echo '<B>saving a File</B><BR>';

	global $fld_file_desc; 
	global $fileUpload; 
	global $fileUpload_name; 
	global $fileUpload_size; 
	global $fileUpload_type; 

	$fld_file_desc = $_POST['fld_file_desc'];

	echo "temp name=" . $_FILES['userfile']['tmp_name'] . "<BR><BR>";

	$str_file_name=  $_FILES['userfile']['name'] ;
	$user_id = $_SESSION['user_id'];

	if ($_FILES['userfile'] != "")
               {



	copy($_FILES['userfile']['tmp_name'], "C:\\apache\\mytemp".$_FILES['userfile']['name']);

	// after paste

	$file_path = "C:\\apache\\mytemp".$_FILES['userfile']['name'];

	echo "file path = $file_path". "<BR><BR>";

	$fileHandle = fopen($file_path, "r"); 
	$fileContent = fread($fileHandle, $_FILES['userfile']['size']);
	$fileUpload_type = $_FILES['userfile']['type'];
	 
	$fileContent = addslashes($fileContent); 

	//echo "<BR>File Content = " . $fileContent;

	$db_server = "localhost"; 
	$db_database = "my_database_name_here"; 
	$db_user = "my_username_here"; 
	$db_pass = "my_password_here";

	$serv_conn = mysql_connect($db_server,$db_user, $db_pass);

	$db_conn = mysql_select_db($db_database, $serv_conn);

	$str_sql = "INSERT INTO tbl_data_pics ";
	$str_sql.="(fld_pic_data,fld_pic_type, fld_pic_name, fld_user_id, fld_pic_label) ";
 
	$str_sql .= " VALUES ( '$fileContent', '$fileUpload_type',";


	
	$str_sql .= " '$str_file_name','$user_id','$fld_file_desc')"; 


	//echo "Query = " . $str_sql . "<BR>";


	mysql_query($str_sql) or die("not gonna happen" . mysql_error());

	

	echo "all done<BR>";
	
	



	//mysql_free_result($result);
	mysql_close($serv_conn);


	//echo "Redirect disabled for debug. Isn't that nice?<BR><BR>";

	//header('Location: gallery.php'); 

	
	//echo "<A href = 'show_files.php'>show</a>";

	echo "size of file: " . strlen($fileContent) . "<BR><BR>";
	

	ob_end_flush();
		  

	}