Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

php - How to display all the images stored inside a database

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...