2012-04-19 153 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;