开发者

Save image as a blob type

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)
开发者_Go百科            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;


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

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


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


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


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']; 
 ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜