Not able to upload a file above 1MB in my MYSQL database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sushant29
    New Member
    • Aug 2012
    • 5

    Not able to upload a file above 1MB in my MYSQL database

    This is an coding for uploading of word,pdf,and ppt file in an database. where as the file below 1Mb are stored in database but the file above 1mb show the below error........

    Error........
    Warning: mysql_query() [function.mysql-query]: MySQL server has gone away in C:\wamp\www\WOR K CENTER\profile. php on line 21

    Warning: mysql_query() [function.mysql-query]: Error reading result set's header in C:\wamp\www\WOR K CENTER\profile. php on line 21

    Code:
    <?php
    			// uploadAction.php
    			include('config.php');
    			if($_SERVER["REQUEST_METHOD"] == "POST")
    				{
    				if (($_FILES["form_data"]["type"] == "application/pdf")
    					|| ($_FILES["form_data"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
    					|| ($_FILES["form_data"]["type"] == "application/msword")
    					|| ($_FILES["form_data"]["type"] == "application/vnd.openxmlformats-officedocument.presentationml.presentation")
    					|| ($_FILES["form_data"]["type"] == "application/vnd.ms-powerpoint"))
    				{
    				$id= mysql_insert_id(); 
    				$file = $_FILES['form_data']['name'];
    				$file_tmpName  = $_FILES['form_data']['tmp_name'];
    				$file_size = $_FILES['form_data']['size'];
    				$file_type = $_FILES['form_data']['type'];
    				$file_section = $_POST["section"];
    				$data=addslashes(fread(fopen($file_tmpName,"r"),filesize($file_tmpName)));
    
    						$queryUser = "INSERT INTO upload (id,data,name,size,type,section) VALUES('$id','$data','$file', '$file_size','$file_type','$file_section')";
    					$insert = mysql_query($queryUser);
    					
    						if(!$insert) 
    						{
    							echo "<div class='f_upload'>upload fail</div>";
    						}
    						else 
    						{
    							echo "<div class='f_upload'>file uploaded successfully</div>";
    						}
    				}  
    				else
    				{
    				 echo "<div class='f_upload'>Invalid file Plz check ur file</div>";
    				}  
    			}
    		?>
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    This is a configuration issue on the MySQL server. By default it only allows packets that are 1 MiB large. When using the old MySQL API to send files larger than that injected into the string, you need to adjust that setting to allow for larger packets.

    See: C.5.2.10. Packet too large.


    By the way, the method you are using to fetch the file data for your query is NOT the proper way to do that. When injecting binary data into a MySQL query, it must be passed through the mysql_real_esca pe_string function first. Otherwise the binary data may corrupt the query and cause it to fail. (And no, the addslashes function is not enough to prevent that, no matter what 10 year old post you copied that code from!)

    Also, you can replace the whole fread(fopen($fi le_tmpName,"r") ,filesize($file _tmpName) mess with the file_get_conten ts($file_tmpNam e) function.

    Comment

    Working...