图像无法从 php 中的数据库显示,并且会引发此类错误。当我执行此代码时,会发生此类错误图像无法显示,因为它包含错误。图像存储在 mysql 数据库中,字段类型为 BLOB。IT 没有找到任何解决方案来避免此错误。
<?php
require_once('database/db.class.php');
$objDatabase=new db_class();
$objDatabase->connect();
$sql="SELECT id,firstname,lastname,sex,email,userimage,ext,city,state,username FROM t_users WHERE username='".$_SESSION['username']."'";
$totalRow=$objDatabase->select($sql);
if($row=$objDatabase->get_row($totalRow, 'MYSQL_BOTH'))
{
$userArray=array('id'=>$row['id'],'firstname'=>$row['firstname'],'lastname'=>$row['lastname'],'sex'=>$row['sex'],'email'=>$row['email'],'userimage'=>$row['userimage'],'ext'=>$row['ext'],'city'=>$row['city'],'state'=>$row['state'],'username'=>$row['username']);
}
?>
<table cellspacing="10px" width="100%">
<tr>
<td width="23%" style="vertical-align:top">
<?php
header("Content-type: image/{$userArray['ext']}");
header("Content-Length: " . strlen($userArray['userimage']));
echo $userArray['userimage'];
?>
</td>
</tr>
</table>
上传图片代码
@list(, , $imtype, ) = getimagesize($_FILES['photo']['tmp_name']);
// Get image type.
// We use @ to omit errors
if ($imtype == 3) // cheking image type
$ext="png"; // to use it later in HTTP headers
elseif ($imtype == 2)
$ext="jpeg";
elseif ($imtype == 1)
$ext="gif";
else
$msg = 'Error: unknown file format';
$img = file_get_contents($_FILES['photo']['tmp_name']);
$img = mysql_real_escape_string($img);
// Preparing data to be used in MySQL query
$data=array('firstname'=>$_REQUEST['firstname'],'lastname'=>$_REQUEST['lastname'],'sex'=>$_REQUEST['gender'],'email'=>$_REQUEST['email'],'userimage'=>$img,'ext'=>$ext,'city'=>$_REQUEST['city'],'state'=>$_REQUEST['state'],'zipcode'=>$_REQUEST['zipcode'],'username'=>$_REQUEST['usernameText'],'password'=>$_REQUEST['passwordText']);
$result=$objDatabase->insert_array('t_users',$data);
<input name="photo" type="file" id="photo">