以下是我用来尝试从数据库中检索图像的代码:
<?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 :(得分: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);
}
?>
答案 1 :(得分: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;