如何显示存储在数据库内的所有图像 [英] How to display all the images stored inside a database

查看:133
本文介绍了如何显示存储在数据库内的所有图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个使用MySQL数据库的画廊(是的,我知道这是一个不好的做法,但它是暂时的要求。)我可以上传多张图片,但我有显示存储在数据库中的所有图像的麻烦。 FORM允许上传五个图像。然后,用户必须前进到另一个页面,其中数据库中的所有图像(包括最近上传的图像)将与图像的描述一起显示。我已经有代码了,但是在显示器上工作的代码不工作,或者我认为是错误的。

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.

这里是表单代码:

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

这是要上传的脚本:

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

最后应该显示:

<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.

推荐答案

从我的理解你的帖子是上传和存储不是一个问题,但显示的图像是。这可能是因为你使用的是未设置的vars,所以在数据库中没有找到结果。如果我误解了,让我知道。

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

还要看看Prof83说的话,如果你的脚本只用一个图片,就忽略我的帖子。

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

最后但同样重要的是,如果你使用不同的文件类型,也会在标题中回显正确的MIME格式。

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

/ strong>
我结合了两个答案。

Update I combined both answers.

编辑您的循环:

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

创建页面名称img.php

Create a page name 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'];
?>

这篇关于如何显示存储在数据库内的所有图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆