我在 php 中显示图像时出错,我的脚本以前可以工作,但现在不行了 :(。
我的脚本是这样的:
$image_id = intval($_REQUEST['id']);
//Query database for our images data and grab it fromt the array returned
$result = mysql_query(sprintf("SELECT data, file_size, mime_type FROM _system_image WHERE id = %d;", $image_id));
$row = mysql_fetch_array($result);
//Set the headers so the browser knows an image is coming and then echo out the data
header("Content-type: " . $row['mime_type']);
header("Content-length: " . $row['file_size']);
echo $row['data'];
exit();
我上传照片的脚本是:
//Create an array of potential errors for uploading images
$image_uploading_errors = array(1 => 'Maximum file size in php.ini exceeded',
2 => 'Maximum file size in HTML form exceeded',
3 => 'Only part of the file was uploaded',
4 => 'No file was selected to upload.');
//Get the filename and size (validation of size is done in html part!)
$filename = $_FILES[SINGLE_IMAGE_UPLOAD_NAME]['name'];
$img_size = $_FILES[SINGLE_IMAGE_UPLOAD_NAME]['size'];
//Use the tmp name, to 'getimagesize' which returns an array of info (not just images size!) and also to get the data from the image
$info = getimagesize($_FILES[SINGLE_IMAGE_UPLOAD_NAME]['tmp_name']);
$data = file_get_contents($_FILES[SINGLE_IMAGE_UPLOAD_NAME]['tmp_name']);
//Get the mime type from the img_size (an array of info)
$mime_type = $info['mime'];
//Put all the info into a new image object
//(img_id, album_id, album_pos, name, low_res_mime, low_res_size, high_res_mime, high_res_size, available_sizes, date_uploaded, date taken, caption)
$img = new Image(NULL, NULL, NULL, $filename, NULL, NULL, $mime_type, $file_size, NULL, new DateTime(), NULL, NULL);
$img->set_high_res_data($data);
//Return the image!
return $img;
使用的 Image 类是我自己编写的,并且像 $mime_type 这样的所有变量都可以正确地进入数据库。
我觉得这可能是一个重复的问题,但我看过这里,没有一个解决方案(我发现的)有用!
编辑:
错误是“无法显示图像'...',因为它包含错误。” 其中“...”是文件名。
我通常尝试使用 html 显示图像:其中 %d 是整数。
问题是图像根本没有得到 siaplyed。
SINGLE_IMAGE_UPLOAD_NAME 是 $_FILES 中数组键的常量。
编辑:
不是一个真正的解决方案,但我改用该方法在这个问题中显示图像,现在它可以工作了: