我在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显示图像:wher%d是一个整数。
问题在于图像根本没有被玷污。
SINGLE_IMAGE_UPLOAD_NAME是$ _FILES中数组键的常量。
编辑:
不是真正的解决方案,但我改为使用该方法在这个问题中显示图像,现在它可以工作: