2

i have written a php code to upload .pdf file into my datbase. the code is working perfect for all pdfs which are less than 1 mb in size.but whenever i am trying to upload a file larger than 1 mb in size , it's failing to upload and throwing a error mysql server has gone away.

NOTE:: i have changed my php.ini with following changes

post_max_size=128M
upload_max_filesize=128M
memory_limit = 128M
max_execution_time = 300
max_input_time = 300

but it's not solving my problem.the problem remains same "it's failing to upload and throwing a error mysql server has gone away." MY PHP CODE IS ::

if(isset( $_POST['save']) and $_POST['save'] == "save")
{
  ini_set('default_socket_timeout', 1500);
  include_once $_SERVER['DOCUMENT_ROOT'].'/include/db.inc.php' ;
  if (!is_uploaded_file($_FILES['upload']['tmp_name']))
  {
    echo 'There was no file uploaded!';
    exit();
  }
  /*NOW I HAVE WRITTEN THE PHP CODE TO INSERT THE FILE INTO MY DATABASE.WHAT I CAN
  ASSURE YOU THIS PART IS PERFECT BECAUSE THE CODE IS SUCESSFUL ALL OTHER CASES
  WHICH ARE LESS THAN 1 MB IN SIZE*/

NOTE:: i also used a reconnect while sending the query.so there is not a connection problem

EDIT there is nothing like max_allowed_packet in my my.ini

RbG
  • 3,181
  • 3
  • 32
  • 43
  • What is the **exact** error that MySQP / PHP returns? – ChrisW Mar 15 '13 at 14:03
  • mysql server has gone away – RbG Mar 15 '13 at 14:04
  • What type of field are you trying to store the pdf in? – Pitchinnate Mar 15 '13 at 14:07
  • i think longblb supports file upto 4.3 gb – RbG Mar 15 '13 at 14:09
  • I have a pathological dislike of storing binary files in a database. There are usually good reasons why it's a bad idea. Seriously, save the file to disk as a normal file; all the DB needs to know is a path to the file. Saving it to DB introduces all kinds of performance, security and complexity problems that simply don't need to happen. – SDC Mar 15 '13 at 14:58

2 Answers2

8

I believe you also have to configure the MySQL server (my.ini on Windows) make sure there is:

[mysqld]
    max_allowed_packet=16M

in there - or any sufficiently high number

also note the same problem solved here: how to change max allowed packet size

Community
  • 1
  • 1
Martina
  • 1,634
  • 1
  • 10
  • 6
  • 1
    @RitabrataGautam As it is a problem regarding MySQL settings i would say it should be sufficient to restart only the MySQL server – Martina Mar 15 '13 at 14:11
  • i am sorry..i have very few knowledge regarding mysql..can u advise me how to restart mysql server – RbG Mar 15 '13 at 14:13
  • 1
    @RitabrataGautam Assuming you are on Windows XP, go to Control panel / Administrative Tools / Services. There locate the MySQL service in the list and chose to "restart" - or - if you can not find it, after changing the my.ini file restart your computer - the service will restart with it :) – Martina Mar 15 '13 at 14:15
  • now what i am going to say is bery surprising.in my "my.ini"..there is nothing like `max_allowed_packet` .... ican't find it – RbG Mar 15 '13 at 14:23
  • 1
    @RitabrataGautam : That is not surprising at all. That only means mysql is using the default value of 1M - You will need to insert the line max_allowed_packet=16M somewhere into the my.ini file UNDER the section of [mysqld]. Also make sure you do the change in that my.ini file that counts (there may be several copies of it on your harddrive) – Martina Mar 15 '13 at 14:26
  • thanks ,thanks and thanks..am literally grateful to u..it was a pain in the neck..solved it..lots of thanks sister – RbG Mar 15 '13 at 14:37
0

There are quite a few things that can cause this, you can get a good overview here: http://dev.mysql.com/doc/refman/5.1/en/gone-away.html

You can start by trying this:

ini_set('mysql.connect_timeout', 300);
ini_set('default_socket_timeout', 300);
zajd
  • 761
  • 1
  • 5
  • 18