2012-09-19 151 views
-4

我已经上传图像数据库,但是当我想显示它的图像不能显示..我不知道它为什么不能显示..可能在我的编码有问题..你可以帮助我??图片无法显示

upload.php的

<?php 

$id = $_POST['account']; 
$code = $_POST['code']; 
$price = $_POST['price']; 

echo $file = $_FILES['image']['tmp_name']; 
if (!isset($file)) 
    echo "Please select an image."; 
else 
{ 
$image = addslashes (file_get_contents($_FILES['image']['tmp_name'])); 
$image_name = addslashes ($_FILES['image']['name']); 
$image_size = getimagesize($_FILES['image']['tmp_name']); 

if($image_size==FALSE) 
    echo "That's not an image."; 
else 
{ 
if (!$insert = mysql_query("INSERT INTO menu 
    VALUES('$code','$price','$image','$id')")) 
echo "Problem uploading images."; 
else 
{ 
    $lastid = $code; 
    echo "Image uploaded.<p />Your image:<p /><img src=get.php?id=$lastid>"; 
} 

    } 
} 
?> 

get.php

<?php 
$con = mysql_connect("localhost","root",""); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db("food", $con); 


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

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

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

echo $image; 
?> 
+0

你可以在这里粘贴什么错误显示? –

+0

为什么你将图像内容存储在数据库中?保存图像的列的类型是什么? –

+0

BLOB DATA TYPE .. – user1683166

回答

0

请确认通过您的数据库,该图像已成功存储手动寻找,并INFACT可读?张贴存储内容图片。

仅供参考,通常最好是将图像存储在服务器上的文件夹中,而不是存储在数据库中,并将路径存储在数据库中。

如果你想存储的路径图像,像这样的工作:

<?php 

$id = mysql_real_escape_string($_POST['account']); 
$code = mysql_real_escape_string($_POST['code']); 
$price = mysql_real_escape_string($_POST['price']); 

$allowedExts = array("jpg", "jpeg", "gif", "png"); 
if(!isset($_FILES)) 
{ 
    die('No Image Uploaded - Please check form enctype="multipart/form-data".'); 
} 
$fileinfo = pathinfo($_FILES['image']['name']); 
$extension = $fileinfo['extension']; 
if(!in_array($extension,$allowedExts)) 
{ 
    die('Invalid file type.'); 
} 
$file = '/uploadedimages/' . uniqid() .'.'. $extension; 
if(!move_uploaded_file($_FILES['image']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . $file)) 
{ 
    die('Problem Storing Image in folder /uploadedimages/ - Please check folder exists and is writable.'); 
} 

$insert = mysql_query("INSERT INTO menu VALUES('$code','$price','$file','$id')"); 
if(!$insert) 
{ 
    die('Problem adding to db.'); 
} 
/* 
the following two echos demostrate two methods of showing the image, 
the first adds the variable by splitting the string and rejoining it, 
the second nests the image inside the variable without the need to edit it. 

The difference is the single and double quotes, single quotes take any content 
inside them as litteral, meaning 
    $string = "hello"; 
    $new_string = '$string Dave.'; 
    will print $string Dave. Not hello Dave. 
Double quotes will accept variables and add the content they hold. 
    $string = "hello"; 
    $new_string = "$string Dave."; 
    will print hello Dave. 
*/ 
echo 'Image Uploaded.<p>Your Image</p><img src="' . $file . '" />'; 

echo "Image Uploaded.<p>Your Image</p><img src=\"$file\" />"; 

的文件夹/ uploadedimages /和将需要写使能(chmod777)

从另一个文件调用; 您可以随时使用您之前使用的查询从数据库调用图像;

$res = mysql_query("SELECT * FROM menu WHERE FoodId=$id"); 
$row = mysql_fetch_assoc($res); 
$image = $row['image']; 
?> 
<img src="<?=$image?>" /> 

很明显,$ id是你已经在php页面上定义的东西。

编辑:其他人向我发布的文章是不必要的 - 使用带变量的单引号并没有什么错误,它清楚地显示了你在哪里添加变量。如果你想展示如何使用双引号你应该提供两种选择并解释不同之处。你的编辑很俗气,写得很糟糕,在双引号之后增加一个空格。

+0

我不明白什么chmod777?并在我的数据库中,我必须使用哪些数据类型来保存图像? BLOB还是什么? – user1683166

+0

你可以使用varchar(70)。 chmod777将您存储图像的目录设置为可写,否则它将不会被写入。您可以在FTP应用程序中执行此操作,通常通过右键单击文件夹并更改权限并勾选所有选项写入读取执行 –

+0

我假设您在Linux网络服务器上 - chmod仅适用于Linux,有一个相当于Windows。 –

0

你为什么不闭上你的img元素?

也尤为明显,让你VAR出字符串的:

echo "Image uploaded.<p>Your image:</p><img src=\"get.php?id=$lastid\" />"; 
+0

它不工作... – user1683166

2

1)记住little Bobby tables;始终将输入清理到数据库中。

2)不要使用旧mysql_* functions,它们是不安全的,贬值(见红色框here)。相反,看看使用PDO或MySQLi,他们不需要很长时间学习,并且在各方面都更好,包括一旦习惯它们后便于使用。

3)将图片保存在服务器上并仅将图片url存储在数据库中会好得多。这是有很多原因的;不仅如此,如果您允许用户上传大量图像,那么当您拥有超过一定数量的图像时,您的数据库将会有几Gb的大小,读取速度非常慢,而且更难定期备份。

上传文件到您的服务器:How to upload & Save Files with Desired name

然后你只需要在文件名存储在数据库中。然后,您可以从数据库中检索文件名,并在需要显示该图像时将其添加到应用程序中的img标记中。

+0

我不知道编码.. – user1683166

+0

哪位? PDO/mySQLi或PHP - 如何使用Desired name链接上传和保存文件,或从数据库中获取URL? – Stu