2014-06-14 19:09
浏览 106


I have uploaded some images to MySQL using PHP. I can also display 1 image everytime i modify the id in the HTML img tag. But now I'm trying to display all images stored in MySql database and the problem is when i use a 'While Loop' it only shows the text columns and not the images stored as BLOB data in MySQL...

I have a database called: my_db I have a table called: blob And i have 3 columns in my table: id, name & image

here is the code for the index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<form action="index.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="image"><input type="submit" name="submit" value="Upload">


    $imageName = mysql_real_escape_string($_FILES["image"]["name"]);
    $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
    $imageType = mysql_real_escape_string($_FILES["image"]["type"]);

    if(substr($imageType,0,5) == "image")
        mysql_query("INSERT INTO `blob` VALUES('','$imageName','$imageData')");
        echo "Image Uploaded!";
        echo "Only images are allowed!";



<img src="showimage.php?id=11">


And here is the code for showimage.php:



    $id = mysql_real_escape_string($_GET['id']);
    $query = mysql_query("SELECT * FROM `blob` WHERE `id`='$id'");
    while($row = mysql_fetch_assoc($query))
        $imageData = $row["image"];
    header("content-type: image/jpeg");
    echo $imageData;
    echo "Error!";


Thanks in advance :-)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtcu5885 2014-06-14 19:18

    First you need to remove the if(isset($_GET['id']) statement because you want to display all images.

    Next, query all images by changing your query to query without an id

    $query = mysql_query("select * from `blob`");

    Next, store the image data to an array.

    $images = array();
    while ($row = mysql_fetch_assoc($query)) {
      $images[] = $row['image'];

    Finally, display all images. (Got the code to display multiple blobs from here)

    /* header should be removed
       header("content-type: image/jpeg"); */
    foreach ($images as $image) {
      echo '<img src="data:image/jpeg;base64,'. base64_encode($image) .'" />';
    打赏 评论
  • drpsrvu85668 2014-07-29 11:33

    In Controller:

    function display()
        $imagename= mysql_real_escape_string($_FILES["userfile"]["name"]);
        $imagedata= mysql_real_escape_string(file_get_contents($_FILES["userfile"]["tmp_name"]));
        $imagetype= mysql_real_escape_string($_FILES["userfile"]["type"]);
        $imagesize= mysql_real_escape_string($_FILES["userfile"]["size"]);
        $imagedetails = array(  'image_name'    => $imagename,
                                    'image_data'   => $imagedata
       // $insertedid =$this->model_data->insert_image($imagedetails);
        $data['imagee'] = $this->model_data->display_pic($insertedid);  // variable data holds image                

    In model:

    function display_pic($insertedid)
        $query = mysql_query("SELECT * FROM employee_image WHERE id = ".$insertedid);
        $row = mysql_fetch_assoc($query);
        return $row['image_data'];
    function insert_image($imagedetails) { 
        return $id;
    打赏 评论
  • dongnuochen9449 2014-12-31 07:16

    why this code is not run in codeigniter when i remove base64_encode so this code run is perfectly and also same thing in file_get_contents,why both function are not run in codeigniter

    $imagedata= mysql_real_escape_string(base64_encode(file_get_contents($_FILES["userfile"]["tmp_name"])));
    打赏 评论

相关推荐 更多相似问题