Php 图像不能显示

Php 图像不能显示,php,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 imag

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

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>";
}

   }
 }
 ?>

为什么不关闭img元素

还要更好地将var从字符串中删除:

echo "Image uploaded.<p>Your image:</p><img src=\"get.php?id=$lastid\" />";
echo“上传图像。您的图像:

”;
请通过手动查看数据库确认图像已成功存储,并且事实是可读的,然后发布存储图像的内容

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

如果要存储图像的路径,可以使用以下类似方法:

<?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\" />";
记住;始终清理数据库中的输入

2) 不要使用旧的
mysql_*函数
,它们是不安全的,并且会贬值(请参见红色框)。相反,看看使用PDO或MySQLi,它们不会花很长时间学习,而且,依我看,它们在各个方面都要好得多,包括一旦你习惯了它们的易用性

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

正在将文件上载到服务器:


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

您可以将其粘贴到此处吗?显示了什么错误?为什么要将图像内容存储在数据库中?保存图像的列的类型是什么?获取图像内容时,请删除
addslashes
函数。应插入二进制数据,而不改变图像,以便正确显示哪个位?PDO/mySQLi,或者PHP-如何上传和保存带有所需名称链接的文件,或者从数据库中获取url?我不明白chmod777是什么??在我的数据库中,我必须使用什么数据类型来保存这些图像??BLOB还是什么?你可以使用varchar(70)。chmod777将存储图像的目录设置为可写,否则将无法写入。您可以在FTP应用程序中执行此操作,方法通常是右键单击文件夹并更改权限,并勾选所有选项“写-读-执行”。我假设您在linux Web服务器上-chmod仅适用于linux,但有windows等效项。警告:move_uploaded_file()[函数。move uploaded file]:无法将第56行C:\wamp\www\Web Staf\phpHidangan.php中的“C:\wamp\tmp\php952.tmp”移动到“C:/wamp/www//uploadedimages/5059d021107ba.jpg”。确定这是因为您在windows上,并且是一个子文件夹,因此我假设您已经在
C:\wamp\www\Web Staf\
中创建了
上载图像
文件夹,您需要从
$file='/uploadedimages/'调整上面的脚本。uniqid()$扩展
$file='/Web Staf/uploadedimages/'。uniqid()$扩展
<?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\" />";
$res = mysql_query("SELECT * FROM menu WHERE FoodId=$id");
$row = mysql_fetch_assoc($res);
$image = $row['image'];
?>
<img src="<?=$image?>" />