2011-05-18 81 views
2

我已經使用MySQL將圖像保存爲Blob類型。我正在通過PHP上傳文件,當我得到圖像時,我只復活了一部分。我怎樣才能提高最大尺寸? (我的圖片文件大小小於300 KB)將圖像保存爲Blob類型

PHP上傳...

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>"; 
      } 
     } 
    } 
    } 

檢索圖像

$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; 
+0

向我們展示一些代碼,請。 – smottt 2011-05-18 17:29:02

+0

整個文件是否正確存儲在數據庫中,檢索是否是問題?或者,文件在數據庫中被截斷了嗎? – 2011-05-18 17:29:22

+0

不知道發生了什麼事。如何識別整個文件是否在數據庫中? – 2011-05-18 17:43:44

回答

1

使用mysql_real_escape_string()逃避,而不是addslashes()圖像數據。 addslashes()不適用於二進制。

+0

作出了改變,但同樣的問題。 – 2011-05-18 17:53:34

+1

嗯。您可以使用MEDIUMBLOB或LONGBLOB類型更改blob列的最大長度。參見[MySQL關於存儲需求的文檔](http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html)。 – Dan 2011-05-18 17:57:11

+0

它與LONGBLOB :)工作正常 – 2011-05-18 18:08:36

-2

你可以使用這個下面的代碼::

<?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

使用像這樣的:

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

這裏,$row['id']是創紀錄的首要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); 
} 

它爲我的作品與WAMP 2.X包