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

我很肯定我做的一切都是正确的……不知道发生了什么。帮助任何人?提前致谢!

4

2 回答 2

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);
}
?>
于 2012-04-19T03:40:08.377 回答
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;
于 2012-04-19T05:51:40.823 回答