当我试图从我的开发环境中的数据库加载图像时,无法加载图像,而是返回错误消息“图像无法显示,因为它包含错误。”
<?php
require_once 'app_config.php'; //app config file
require_once 'database_connection.php'; //database connection
try{
//Get the image id.
if(!isset($_REQUEST['image_id'])){
handle_error("No image to load was specified.");
}
$image_id = $_REQUEST['image_id'];
//Build the SELECT statement
$select_query = sprintf("SELECT * FROM images WHERE image_id = %d", $image_id);
//Run the query
$result = mysql_query($select_query);
//Get the result and handle errors from getting no result
if(mysql_num_rows($result) == 0){
handle_error("We couldn't find the requested image.", "No image found with and ID of " . $image_id . ".");
}
$image = mysql_fetch_array($result);
//Tell the browser what's coming with headers
header('Content-type: ' . $image['mime_type']);
header('Content-length: ' . $image['file_size']);
echo $image['image_data'];
}catch(Exception $exc){
handle_error("Something went wrong loading your image.",
"Error loading image: " . $exc->getMessage());
}
?>