无法在PHP中显示图像
这是我的图像显示代码-无法在PHP中显示图像,php,mysql,image,lamp,Php,Mysql,Image,Lamp,这是我的图像显示代码- $username = "xxxxxxxx"; $password = "xxxxxxxx"; $host = "000.001.000.000"; $database = "xxxxxxxx"; @mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error()); @mysql_select_db($database) or
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$query = mysql_query("SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1");
$row = mysql_fetch_array($query);
$content = $row['Image'];
header('Content-type: image/jpg');
echo $content;
这就是我得到的错误
“形象”http://www....“无法显示,因为它包含错误
怎么了?mysql中字段的数据类型是mediumblob我建议在输出之前添加一个内容长度头:
header("Content-length: " . strlen($content))
我建议根据图像mime类型更改标题,使其成为动态的:
header('Content-type: '.$row['mimetype']);
好的,第一个测试,看看发生了什么:
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";
if( !mysql_connect($host, $username, $password) )
die( 'Unable to connect to Server: '.mysql_error() );
if( !mysql_select_db($database) )
die( 'Can not select the Database: '.mysql_error() );
$query = mysql_query( "SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1" );
if( !$query )
die( 'Query Failed: '.mysql_error() );
if( mysql_num_rows( $query )==0 )
die( 'Query Returned No Records' );
$row = mysql_fetch_array($query);
echo '<pre>';
var_dump( $row );
echo '</pre>';
(假设
mimetype
字段类似于“image/jpg”)图像文件有多大?在添加到数据库之前是否对其进行编码(例如mysql_escape_chars?)?是否确定显示的图像是jpeg?您正在获取mimetype和image,但没有使用mimetype-这告诉我您可能会存储多个文件类型。(gif/jpg/png等)注释掉头()
位,然后再次检查页面是否有错误。然后去掉那些错误抑制器。数据库、表和列都是utf8??
$username = "xxxxxxxx";
$password = "xxxxxxxx";
$host = "000.001.000.000";
$database = "xxxxxxxx";
if( !mysql_connect($host, $username, $password) )
die( 'Unable to connect to Server: '.mysql_error() );
if( !mysql_select_db($database) )
die( 'Can not select the Database: '.mysql_error() );
$query = mysql_query( "SELECT mimetype, Image FROM table ORDER BY id DESC LIMIT 0,1" );
if( !$query )
die( 'Query Failed: '.mysql_error() );
if( mysql_num_rows( $query )==0 )
die( 'Query Returned No Records' );
$row = mysql_fetch_array($query);
header( 'Content-type: '.$row['mimetype'] );
echo $row['Image'];