2012-04-19 155 views
0

這裏是我使用的嘗試,並從數據庫中檢索圖像的代碼:檢索圖像 - 不顯示圖像

<?php 
if($id) 
{ 
    //please change the server name username and password according to your mysql server setting 
    $mysql_server="localhost"; 
    $mysql_username="myuser"; 
    $mysql_password="mypass"; 
    $mysql_database="mydb"; 
    //connect to database using above settings 
    @MYSQL_CONNECT("localhost",$mysql_username,$mysql_password); 
    @mysql_select_db("mydb"); 
    //select the picture using the id 
    $query = "select bin_data,filetype from todo where id=$id"; 
    //execute the query 
    $result = @MYSQL_QUERY($query); 
    //get the picture data which will be binary 
    $data = @MYSQL_RESULT($result,0,"bin_data"); 
    //get the picture type. It will change according to file extension it may be either gif or jpg 
    $type = @MYSQL_RESULT($result,0,"filetype"); 
    //send the header of the picture we are going to send 
    Header("Content-type: $type"); 
    //send the binary data 
    echo $data; 
}; 


?> 

而不是顯示上述請求圖像時,會顯示該圖標:(不知道你們怎麼稱呼它)...... http://i.imgur.com/bo6Jg.png

這裏是我的表中的所有列:http://i.imgur.com/PuWvl.png

我敢肯定的,我做的一切權利...不知道是怎麼回事。幫助任何人?提前致謝!

+0

擺脫那些邪惡的@標誌!並刪除最後一個結束標記:?> – 2012-04-19 03:24:14

+0

這些mysql函數名稱區分大小寫,什麼/誰給你的想法,你可以大寫他們,抑制錯誤也是crackers當你試圖調試..也'$ id'是空白所以它永遠不會那麼遠,你應該使用'isset($ id)&& is_numeric($ id)'來代替。 – 2012-04-19 03:26:22

+0

@LawrenceCherone在php中的函數名稱*不區分大小寫,但我仍然認爲他們應該像他們一樣對待。 – 2012-04-19 03:32:58

回答

1

IMO唯一不變的應該是大寫的(儘管我很樂意不知道他們可能是上),

反正試試這個:

<?php 
$file_not_found = '../not_found_image.jpg'; 
//Get the id param from GET else null 
$id = (isset($_GET['id']) && is_numeric($_GET['id']))?$_GET['id']:null; 

if($id != null) { 
    $mysql_server="localhost"; 
    $mysql_username="myuser"; 
    $mysql_password="mypass"; 
    $mysql_database="mydb"; 
    //Connect to database using above settings 
    mysql_connect($mysql_server,$mysql_username,$mysql_password) or die(mysql_error()); 
    mysql_select_db($mysql_database) or die(mysql_error()); 
    //Select the picture using the id 
    $query = "SELECT `bin_data`, `filetype` FROM todo WHERE id=".(int)mysql_real_escape_string($id)." LIMIT 1"; 
    //Execute the query 
    $result = mysql_query($query); 

    //Found 
    if(mysql_num_rows($result)==1){ 
     //Get the picture data which will be binary 
     $data = mysql_result($result,0,"bin_data"); 
     //Get the picture type. It will change according to file extension it may be either gif or jpg 
     $type = mysql_result($result,0,"filetype"); 
     //Send the header of the picture we are going to send + cache 
     header('Cache-Control: private, max-age='.(60*60*24*365)); 
     header('Expires: '.gmdate(DATE_RFC1123,time()+60*60*24*365)); 
     header("Pragma: private"); 

     header('Content-Type: '.$type); 
     header('Content-Length: ' . strlen($data)); 
     //Send the binary data 
     echo $data; 

    }else{ 
    //Not found 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Type: image/jpeg'); 
    header('Content-Length: ' . filesize($file_not_found)); 
    readfile($file_not_found); 
    } 

}else{ 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Pragma: public'); 
    header('Content-Type: image/jpeg'); 
    header('Content-Length: ' . filesize($file_not_found)); 
    readfile($file_not_found); 
} 
?> 
+0

工作!謝謝。 – Alex 2012-04-19 21:19:22

0

試試這個..

header("Content-type: image/gif"); 
$expires = 60*60*24*14; 
header("Pragma: public"); 
header("Cache-Control: maxage=".$expires); 
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT'); 
error_reporting(0); 
require_once "class/dbconn.php"; 
$id=$_GET['id']; 

$sql="select thumbimage from image where img_id=$id"; 
$rs=mysql_query($sql) or die (mysql_error()); 
$row =mysql_fetch_array($rs,MYSQL_BOTH); 
$data = $row[0]; 
print $data;