2

I am trying to display images from my mysql database using php. The image is not getting displayed fully. It gets cut while trying to display an image more than 200 kb (determined from trials , but not too sure).

HTML Code:

<form enctype="multipart/form-data" action="insertimage.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="10240000" type="hidden">
<input name="image" accept="image/jpeg|image/jpg|image|JPG|image/png|image/gif" type="file">
<input value="Submit" type="submit">

PHP Code:

<?php
    require('myconnect.php');
    
    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 
    
              // Temporary file name stored on the server
              $tmpName  = $_FILES['image']['tmp_name'];  
               
              // Read the file 
              $fp     = fopen($tmpName, 'r');
              $data = fread($fp, filesize($tmpName));
              $data = addslashes($data);
              fclose($fp);
              
    
              // Create the query and insert
              // into our database.
              $query = "Update whyangry.posts set Photo='$data' where Pid=2";
              $results = mysql_query($query, $con);
              
              // Print results
              print "Thank you, your file has been uploaded.";
               
    $sql = "SELECT * FROM helpme.posts WHERE Pid=2";
    $res = mysql_query($sql,$con);
    while ($res1=mysql_fetch_assoc($res))
    {   
    $content = $res1['Photo'];
    $id=$res1['Pid'];
    
    }
    echo '<img src="data:image/png|image/jpeg|image/gif;base64,' . base64_encode( $content ) . '" />';
    echo 'Hello world.';
    
    }
    else {
       print "No image selected/uploaded";
    }
    
    ?>

Also i am getting the below error while uploading file in phpmyadmin to a blob datatype

UPDATE `helpme`.`posts` SET `Photo` = 0xffd8ffe000104a46494600010201006000600000ffe10f074578696600004d4d002a0000000800060132000200000014000000564746000300000001000300004749000300000001003200009c9d00010000000e00000000ea1c0007000007f40000000087690004000000010000006a000000d4323030393a30333a31322031333a34373a34330000059003000200000014000000ac9004000200000014000000c0929100020000000335340000929200020000000335340000ea1c0007000007b40000000000000000323030383a30333a31342031333a35393a323600323030383a30333a31342031333a35393a3236000005010300030000000100060000011a00050000000100000116011b0005000000010000011e020100040000000100000126020200040000000100000dd90000000000000048000000010000004800000001ffd8ffe000104a46494600010100000100010000ffdb004300100b0c0e0c0a100e0d0e1211101318281a181616183123251d283a333d3c3933383740485c4e404457453738506d51575f626768673e4d71797064785c656763ffdb0043011112121815182f1a1a2f634238426363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363[...]

MySQL said:

2006 - MySQL server has gone away

Please let me know how to fix the issue. The issue is while displaying images. Whether some size issue is there i dont know please help here.

2
  • 1
    Maybe there is a problem with your maximum query size (dev.mysql.com/doc/refman/5.0/en/packet-too-large.html), as you need to escape the data with slashes, which makes your query extremely large. Commented Aug 18, 2012 at 19:36
  • The error 2006 - MySQL server has gone away means that your server timed out and the connection was closed. Try executing the query again. If you get the same error again, you might have to increase the timeout setting of your MySQL server. Commented Aug 19, 2012 at 5:00

1 Answer 1

1

Using addslashes is nowhere near the correct way to do a SQL query. It will not always work correctly with binary data. I don't know what resource you're using, but it's teaching you very bad habits.

Please DO NOT USE mysql_query in new applications. This is a legacy interface from the 1990s that is in the process of being retired because of the hazards involved in using it incorrectly, something all too easy to do. It's best to use either mysqli or PDO in new projects.

Your query should look like this:

Update whyangry.posts set Photo=? where Pid=?

You can bind to those placeholders when executing the query and avoid having encoding problems. There are many examples on how to do this correctly.

Sign up to request clarification or add additional context in comments.

5 Comments

Oh Thanks for showing me this. But i have made a whole application using mysql instead of mysqli. What are the risks involved now?
To put it lightly, your entire business could be wiped out if you fall victim to a SQL injection bug. You must be sure each and every string you put in your SQL is escaped with mysql_real_escape_string or there could be consequences. This is why making the change to mysqli or PDO, which is safe if used correctly, is important to do now. Plus, when the mysql series of functions are retired, your application will not be affected.
Also the above problem is solved. This helped for me: edit .../xampp/sql/bin/my.ini set max_allowed_packet to e.g. 16M Cheers
Good to hear. Be cautious. It's a scary internet out there.
As a note, you should probably crank max_allowed_packet to something reasonable like 1GB if you're ever loading in backups. mysqldump creates very large INSERT statements by default for optimal performance since it loads in thousands of rows per call. This doesn't use up more memory like persistent buffers do, but simply allows for much larger queries to be processed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.