-2

我创建了这个表单来将我的图像添加到 mysql 数据库中,我认为我做对了——因为它保存了一些东西:D - 但它不会向我显示图像,我应该怎么做才能“查看”来自 mysql 的图像?!

这是我的表单 php:

$tmp_name=$_FILES['file']['tmp_name'];

if (isset($_POST['submit'])) {
if ((($_FILES['file']['type']) == "image/jpeg")
|| ($_FILES['file']['type']) == "image/gif"
|| ($_FILES['file']['type']) == "image/pjpeg"
&& ($_FILES['file']['size']) > 200000) {
$tmp_name=$_FILES['file']['tmp_name'];
// i also tried addslasheds   
$image = mysql_real_escape_string(file_get_contents($_FILES['file']['tmp_name']));
if ($_FILES['file']['error'] > 0) {
    echo "return code : " . $_FILES['FILES']['error'];
}else{
    if (file_exists($_FILES['file']['name'])) {
        echo "your file is already exists!";
    }else{
        Query("INSERT INTO image(image) VALUES ('".$tmp_name."')");
        echo "FILES has been stored";
    }
}
}
}else{
echo "invalid file";
}?>

我显示图像的代码是:

<?php 
require 'lib.php';
$request=Query('SELECT * FROM image');
while ($row = mysql_fetch_array($request)) {
echo $row['image'];
}?>
4

1 回答 1

0

将临时名称插入数据库不会完成任何事情,因为它只是临时名称。您需要将图像保存在服务器上的某个位置move_uploaded_file(),然后将新的永久名称保存到数据库中,以便以后可以在 html 图像标签中使用它。

$filename = "myimage.jpg";
$path = "/var/www/images/".$filename;
$link = "http://domain.com/images/".$filename;
move_uploaded_file($FILES['file']['tmp_name'], $path);
$image = mysql_real_escape_string($link);
if ($_FILES['file']['error'] > 0) {
    echo "return code : " . $_FILES['FILES']['error'];
}else{
    Query("INSERT INTO image(image) VALUES ('".$image."')");
    echo "FILES has been stored";
}

然后,当您检索图像时:

<?php 
require 'lib.php';
$request=Query('SELECT * FROM image');
while ($row = mysql_fetch_array($request)) {
echo '<img src="'.$row['image'].'" />";
}?>
于 2013-08-17T17:39:14.117 回答