V
V
Vanes Ri_Lax2015-11-27 08:25:38
PHP
Vanes Ri_Lax, 2015-11-27 08:25:38

Error writing files to the database, how to solve?

Hello,
I am developing a web interface.
The files need to be stored in the database. (do not swear, this is tz) I
created a table, the column in which is of the longblob type,
configured php to skip files for more,
wrote a simple php script that actually downloads files and writes them to the database.

$allowed = array('png', 'jpg', 'gif', 'jpeg');
if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){
  $extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
  if(!in_array(strtolower($extension), $allowed)){
    echo '{"status":"error"}';
    exit;
  }
  $files = file_get_contents( $_FILES['upl']['tmp_name'] );
  $files = mysql_escape_string( $files );
  $mime = $_FILES['upl']['type'];
  $size = $_FILES['upl']['size'];
  $name = $_FILES['upl']['name'];
  $kode = $_SESSION['kode'];
  
  if(isset($_SESSION['img_id']) and $_SESSION['img_id']!=''){
    $imgID = $_SESSION['img_id'];
    $sql = "INSERT INTO img (content,leng,mime,name,kod_users) VALUES ('$files','$size','$mime','$name','$imgID')";
  }
  else
  {
    $sql = "INSERT INTO img (content,leng,mime,name,kode) VALUES ('$files','$size','$mime','$name','$kode')";
  }
  if(mysqli_query($db,$sql)){
    echo '{"status":"success"}';
    exit;
  }
  else{
    echo mysqli_error($db);
  }
}

The error occurs here:
if(mysqli_query($db,$sql)){
    echo '{"status":"success"}';
    exit;
  }

namely, the
if(mysqli_query($db,$sql))
Client receives status 500
Here is what is in the logs:
PHP Warning:  mysqli_query(): MySQL server has gone away in C:\inetpub\wwwroot\ok\upload.php on line 43
PHP Warning:  mysqli_query(): Error reading result set's header in C:\inetpub\wwwroot\ok\upload.php on line 43
PHP Warning:  mysqli_query(): MySQL server has gone away in C:\inetpub\wwwroot\ok\upload.php on line 43
PHP Warning:  mysqli_query(): Error reading result set's header in C:\inetpub\wwwroot\ok\upload.php on line 43
PHP Warning:  mysqli_query(): MySQL server has gone away in C:\inetpub\wwwroot\ok\upload.php on line 43
PHP Warning:  mysqli_query(): Error reading result set's header in C:\inetpub\wwwroot\ok\upload.php on line 43

What could be the problem? I guess I need to configure something in MySQL itself, but what?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Zelensky, 2015-11-27
@SergeyZelensky-Rostov


taken from here
These errors will also be thrown when an invalid or too long request is sent to the server. If mysqld receives an invalid or too large packet, then the server assumes something is wrong with the client and closes the connection. If you need to run large queries (for example, when dealing with large BLOB columns), you can increase the query size limit by starting mysqld with the -O max_allowed_packet=# option (default 1 MB). Extra memory is allocated on demand, so mysqld will only use more memory when a large query is issued or when mysqld needs to return a large result row!
PS learn to google!!!

D
Dmitry Chilikin, 2016-03-04
@peacock

I had such a task: put jQuery into the database so that the C# application would work with the built-in browser and generate pages. So I put the JQuery library into the database using BASE64 encoding, on reading - the reverse transformation. Try)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question