2

I have used MySQL to save a image as a blob type. I 'm uploading files through PHP and when I get the image back I revive only a part of it. How can I improve the max size ? (my image file size is less than 300 KB)

PHP uploader...

if($_FILES!=null && $_POST!=null){
    $file = $_FILES["image"]["tmp_name"];   

    if(!isset($file)){
        echo "Please upload an image";
    }else{
        $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));

        $image_name = addslashes($_FILES['image']['name']);
        $type=$_POST['type'];

        $image_size = getimagesize($_FILES['image']['tmp_name']);

        if($image_size==FALSE)
            echo "That's not an image.";
        else
        {

            if(!(mysql_query("INSERT INTO store (name,image,type) values  ('$image_name','$image','$type')")))
                echo "Problem uploading image";
            else
            {
                $lastid = mysql_insert_id();
                echo "Image uploaded. <p /> Your image: <p /> <img id='imageId' src=get.php?id=$lastid>";
            }
        }
    }
  }

retrieving image

$id = addslashes($_REQUEST['id']) ;

$imageRow = mysql_query("SELECT * FROM store WHERE id=$id");

$image = mysql_fetch_assoc($imageRow);
$image = $image['image'];

header("Content-type: image/jpg");


echo $image;
5
  • Is the entire file storing correctly in the database and retrieval is the problem? Or, is the file truncated in the database? Commented May 18, 2011 at 17:29
  • don't know what's happening. How do I identify whether entire file is in the DB? Commented May 18, 2011 at 17:43
  • 1
    Take a file of known size and upload it. You can then use phpmyadmin and see the size of the blob in the table. It may be that your database field is too small and files are being truncated. Commented May 18, 2011 at 17:45
  • Truncated in DB max size 64 KB. How can I increase the max length? Commented May 18, 2011 at 17:48
  • Storing images in a database is almost always a bad plan. Rather than doing that I'd suggest storing a pointer to the image in the database and storing the image itself on disk somewhere. Commented May 18, 2011 at 17:56

4 Answers 4

3

You can use different types of blobs. Blob, Mediumblob, longblob, etc.

http://dev.mysql.com/doc/refman/5.0/en/blob.html

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

1 Comment

medium blob 16 Mb. long blob 4 Gb
1

Use mysql_real_escape_string() to escape the image data, instead of addslashes(). addslashes() isn't meant for binary.

2 Comments

made the change but same issue.
Hmmm. You can change the maximum length of a blob column by using MEDIUMBLOB or LONGBLOB types. See MySQL's docs on storage requirements.
0

Use image like this:

<img src='file_display.php?id=<?php echo $row['id']; ?>' width='100' height='100'>

Here, $row['id'] is record primary key - id

and in file_display.php:

// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
    //connect to the db
    $link = mysql_connect($host, $username, $password) or die("Could not connect: " . mysql_error());

    // select our database
    mysql_select_db($database) or die(mysql_error());

    // get the image from the db
    $sql = "SELECT image FROM tbl_images WHERE id=" .$_GET['id'] . ";";

    // the result of the query
    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

    // set the header for the image
    header("Content-type: image/jpeg");
    echo mysql_result($result, 0);

    // close the db link
    mysql_close($link);
}

It works for me with WAMP 2.x package

Comments

-1

you can use this following code ::

 <?php
        mysql_connect("localhost","root","") or die(mysql_error());
        mysql_select_db("database") or die(mysql_error());   

        $id = $_GET['id'];
        $sql = mysql_query(" SELECT * FROM  store WHERE id=$id") or die(mysql_error()); 

        $row = mysql_fetch_array($sql);
        header('Content: image/jpeg');

        echo $row['image']; 
 ?>

Comments

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.