图像无法显示

时间:2012-09-19 13:24:58

标签: php

我已经将图像上传到数据库中,但是当我想要显示它时,图像无法显示..我不知道为什么它无法显示..我的编码可能有问题。你能帮我吗?

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;
 ?>

3 个答案:

答案 0 :(得分:2)

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

2)不要使用旧的mysql_* functions,它们不安全且折旧(请参阅红框here)。相反,使用PDO或MySQLi,它们不需要花费很长时间才能学习,并且在各方面都非常好,包括习惯后的易用性。

3)将图像保存在服务器上并将图像URL存储在数据库中要好得多。这有很多原因;尤其是,如果您允许用户上传大量图像,那么当您拥有多个图像时,您的数据库大小将达到几Gb且读取速度非常慢,甚至更难定期备份。

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

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

答案 1 :(得分:0)

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

还可以让你从字符串中删除var:

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

答案 2 :(得分:0)

请通过手动查看您的数据库确认图像已成功存储,并且实际上是可读的?,发布存储图像的内容。

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

如果你想存储图像的路径,这样的东西就可以了:

<?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页面上定义的东西。

编辑:其他人对我发布的编辑是不必要的 - 使用单引号和变量没有任何问题,它清楚地显示了你在哪里添加变量。如果你想展示如何使用双引用你应该提供两个选项并解释差异。你的编辑很俗气,编写得很糟糕,在双引号后加了一个空格。

相关问题