2014-07-07 206 views
1

我在检索最近保存在数据库中的图像时遇到问题。它,而不是显示这个残缺的图像图标
如何从MySQL中检索图像PHP

表单页面代码:

<form id "formUpload" action="uploadimage.php" method="POST"enctype="multipart/form-data" target="iframe"> 
<input type="file" name="image" id "image" style="background-color:#000"> 
<br> 
<input type="submit" value="Upload"> 
</form> 

<iframe name ="iframe" width="140" height="216"> 
<img style="min-height: 216; max-width: 140; max-height: 216px;" id="image" /> 
<br /> 
<br /> 
</iframe> 


上传图像的代码(这是在不同的文件):
$ lastid被用来获得数据库中最近保存的图像将显示在表单页面上。

include("databaseconnect.php"); 

//file properties 
$file = $_FILES['image']['tmp_name']; 

if(!isset($file)) 
{ 
    echo "Select Image File: "; 
} 

else 
{ 
$image = addslashes($_FILES['image']['tmp_name']); 
$image_name = addslashes($_FILES['image']['imageNAME']); 
$image_size = getimagesize($_FILES['image']['tmp_name']); 

if($image_size==FALSE) 
{ 
echo "thats not an image"; 
} 
else 
{ 
if(!$insert = mysql_query("INSERT INTO tblcinema VALUES ('', '', '', '', '', '', '', '', '', '', '$image', '$image_name', '')")) 
{ 
echo "Upload Image Failed"; 
} 

else 
{ 
$lastid = mysql_insert_id(); 
echo "<img src=uploadimage.php?id=$lastid>"; 
} 
} 
} 


显示图像到表单页面代码(也以不同的文件):

include("databaseconnect.php"); 

$id = addslashes($_REQUEST['imageID']); 

$image = mysql_query("SELECT * FROM tblcinema WHERE imageID = $id"); 
$image = mysql_fetch_assoc($image); 
$image = $image['image']; 

header('Content-type: image/jpeg'); 

echo $image; 


和在数据库表中,我已经使用的类型BLOB。

请帮我解决这个......尽快!!!
我会非常感激:)

回答

0

首先你需要对上传的文件移动到特定文件夹 :

`move_uploaded_file(file,location);` 

检查你在$图像已完成了必须是图像文件的路径,然后 你可以通过它的路径获取图像:

<img style="min-height: 216; max-width: 140; max-height: 216px;" id="image" src="<?php echo $image; ?>" /> 
0

你不是存储实际图像(二进制数据)到数据库中,但tmp_name(串)。使用file_get_contents(),就像这样:

$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); 
0

您没有储存图像的任何地方

您需要从临时文件夹中的图像文件移动到您的上传文件夹,存储路径图像数据库中的

0

先移动上传的文件,然后按路径获取:

if(!isset($file)) 
{ 
    echo "Select Image File: "; 
} 

else 
{ 

    $image = addslashes($_FILES['image']['tmp_name']); 
    $image_name = addslashes($_FILES['image']['imageNAME']); 
    $image_size = getimagesize($_FILES['image']['tmp_name']); 
    $uploaddir = "Directory where files can be store"; 
    $upload_path = $uploaddir.'/'.$image_name; 

    move_uploaded_file($_FILES["image"]["tmp_name"],$upload_path)); 
//other codes goes here 
} 

to get the value of image : 

    <img style="min-height: 216; max-width: 140; max-height: 216px;" id="image" src="folder-path/<?php echo $image; ?>" />