php 如何显示存储在数据库中的所有图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8758548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:26:47  来源:igfitidea点击:

How to display all the images stored inside a database

phphtmlsqldatabaseimage

提问by SimonCode

I am making a gallery that uses a MySQL database (yeah I know it's a bad practice but it's the requirement for the moment.) I can upload multiple images but I'm having trouble displaying all images stored inside the database. The FORM allows five images to be uploaded. Then the user must proceed to another page where all the images in database (including the ones uploaded recently) will be displayed together with the description of the images. I have code already but the one that will work on the display is not working or I think is wrong.

我正在制作一个使用 MySQL 数据库的画廊(是的,我知道这是一个不好的做法,但这是目前的要求。)我可以上传多张图片,但我无法显示存储在数据库中的所有图片。FORM 允许上传五张图片。然后用户必须进入另一个页面,在该页面中将显示数据库中的所有图像(包括最近上传的图像)以及图像的描述。我已经有了代码,但是可以在显示器上运行的代码不起作用,或者我认为是错误的。

Here is the form code:

这是表单代码:

 <html>
 <head>
    <title> Upload image</title>

 </head>
 <body> 
 <div align="center">
    <form action="fUpload.php" method="POST" enctype="multipart/form-data">
    All forms must be filled. <br />
    File: <br />
    <input type="file" name="image[]"/> <input type="text" name="imageDescription[]" size="30" /> <br />
    <input type="file" name="image[]"/>  <input type="text" name="imageDescription[]" size="30" /> <br />
    <input type="file" name="image[]"/>  <input type="text" name="imageDescription[]" size="30" /> <br />
    <input type="file" name="image[]"/>  <input type="text" name="imageDescription[]" size="30" /> <br />
    <input type="file" name="image[]"/> <input type="text" name="imageDescription[]" size="30" /> <br />

    <input type="submit" value="Upload image" />

    </form>
</div>  
</body>
</html>

Here is the script that would upload:

这是将要上传的脚本:

 <?php 

//connect to the database//
$con = mysql_connect("localhost","root", "");
if(!$con)
{
 die('Could not connect to the database:' . mysql_error());
 echo "ERROR IN CONNECTION";
}

$sel = mysql_select_db("imagedatabase");
if(!$sel)
{
 die('Could not connect to the database:' . mysql_error());
 echo "ERROR IN CONNECTION";
}
//file properties//

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

echo '<br />';

 /*if(!isset($file))
    echo "Please select your images";

else
{
 */for($count = 0; $count < count($_FILES['image']); $count++)
{
//$image = file_get_contents($_FILES['image']['tmp_name']);
    $image_desc[$count] = addslashes($_POST['imageDescription'][$count]);
    $image_name[$count] = addslashes($_FILES['image]']['name'][$count]); echo '<br \>';
    $image_size[$count] = @getimagesize($_FILES['image']['tmp_name'][$count]);
    $error[$count] = $_FILES['image']['error'][$count];

    if($image_size[$count] === FALSE  || ($image_size[$count]) == 0)
        echo "That's not an image";
    else
    {

    // Temporary file name stored on the server
     $tmpName[$count]  = $_FILES['image']['tmp_name'][$count];

  // Read the file
    $fp[$count]   = fopen($tmpName[$count], 'r');
    $data[$count] = fread($fp[$count], filesize($tmpName[$count]));
    $data[$count] = addslashes($data[$count]);
     fclose($fp[$count]);


  // Create the query and insert
  // into our database.

  $results = mysql_query("INSERT INTO images( description, image) VALUES             ('$image_desc[$count]','$data[$count]')", $con);

        if(!$results)
        echo "Problem uploding the image. Please check your database";  
    //else 
    //{
        echo "";
        //$last_id = mysql_insert_id();
        //echo "Image Uploaded. <p /> <p /><img src=display.php?    id=$last_id>";
        //header('Lcation: display2.php?id=$last_id');
        }
    //}
}


mysql_close($con);
header('Location: fGallery.php');
?>

And finally the one that should display:

最后是应该显示的:

<html>
<body>

</body>
<?php

//connect to the database//
mysql_connect("localhost","root", "") or die(mysql_error());
mysql_select_db("imagedatabase") or die(mysql_error());

//requesting image id

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

$image = mysql_query("SELECT * FROM images WHERE id = $id");


while($datum = mysql_fetch_array($image, MYSQL_ASSOC))
{
        printf("Description %s $image = $image['image'];

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

}

mysql_close();


?>

Your help is much appreciated. I need it badly to move on.

非常感谢您的帮助。我非常需要继续前进。

回答by mat

From what i understand from your post is that uploading and storing isn't a problem, but showing the images is. That's probably because you're using vars that are not set, so no results kan be found in the database. If i misunderstood let me know.

从我从您的帖子中了解到,上传和存储不是问题,但显示图像是。这可能是因为您使用了未设置的变量,因此在数据库中找不到任何结果。如果我误解了,请告诉我。

<?php
// No ID
$image = mysql_query("SELECT * FROM images ORDER BY id DESC");   
?>

Also look at what Prof83 says. Ignore my post if your script works with just one image.

也看看 Prof83 怎么说。如果您的脚本仅适用于一张图片,请忽略我的帖子。

Last but not least, if you're using different filetypes, also echo the correct MIME format in the header.

最后但并非最不重要的是,如果您使用不同的文件类型,还要在标题中回显正确的 MIME 格式。

UpdateI combined both answers.

更新我结合了两个答案。

Edit your loop:

编辑你的循环:

<?php
while($row = mysql_fetch_assoc($image))
{
        echo '<img src="img.php?id='.$row["id"].'">';
}
?>

Create a page name img.php

创建一个页面名称 img.php

<?php
$query = mysql_query("SELECT image FROM images WHERE id = ".$_GET['id']);
$row = mysql_fetch_assoc($query);
header("Content-type: image/jpeg");
echo $row['image'];
?>

回答by Prof83

Ok you can't display multiple images within a image/jpeg page...

好的,您无法在图像/jpeg 页面中显示多个图像...

You're telling the browser that the page is image/jpeg (in other words, the page is AN IMAGE) but you're echoing out multiple image data

您告诉浏览器该页面是图像/jpeg(换句话说,该页面是 AN IMAGE)但您正在回显多个图像数据

You should rather use the gallery page to show all images like this:

您应该使用图库页面来显示所有图像,如下所示:

<?php
// $images = result from database of all image rows
foreach ($images as $img) echo '<img src="img.php?id='.$img["id"].'">';
?>

and in img.php:

在 img.php 中:

// Load the image data for id in $_GET['id'];
header("Content-type: image/jpeg");
echo $data;