Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a PHP Expert!

Join 244,267 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,195 people online right now. Registration is fast and FREE... Join Now!




How to display images uploaded in directory

 
Reply to this topicStart new topic

How to display images uploaded in directory

Ghost rider
1 Dec, 2008 - 05:44 AM
Post #1

D.I.C Head
**

Joined: 29 Oct, 2008
Posts: 59

Please Help! I've tried everything to sort out the trouble with this code but without result. I don't know what do next. I would appreciate if someone there could help me. Thank in advance.

My upload script is uploaded images file name and others details of images in database and then it save images itself in directory(server).

Then I do have another script to download images files that are in directory and and details of images file that are in database. But the trouble that I'm having at moment when run download script I can seen the links of images details in database but when I clicked to open images I can't view the images that were uploaded.

There is any way I can display images that are in directory and with details of images that are stored in database together when run download script. Please if you have some conceptd about it help. Thanks

Here my download script.

CODE
<?php
error_reporting(E_ALL);
if(isset($_GET['id']))
{
    include 'library/config.php';
    include 'library/opendb.php';

    $id      = $_GET['id'];
    $query   = "SELECT name, type, size, path FROM upload2 WHERE id = '$id' ";
    $result  = mysql_query($query) or die('Error, query failed');
    list($name, $type, $size, $filePath) = mysql_fetch_array($result);

    header("Content-Disposition: attachment; filename=$name");
    header("Content-length: $size");
    header("Content-type: $type");
    
    readfile($filePath);

    include 'library/closedb.php';    
    exit;
}

?>

<html>
<head>
<title>Download File From File Server</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'library/config.php';
include 'library/opendb.php';

$query  = "SELECT id, name FROM upload2";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
    echo "Database is empty <br>";
}
else
{
    while(list($id, $name) = mysql_fetch_array($result))
    {
?>
    
    <br>
    <img src="download2.php?id=<?php echo $id ?>" alt="Images" />

    //<a href="download2.php?id=<?php echo $id; ?>"><?php echo $name; ?></a> <br>
<?php        
    }
}
include 'library/closedb.php';
?>
</body>
</html>


User is offlineProfile CardPM
+Quote Post


Martyr2
RE: How To Display Images Uploaded In Directory
1 Dec, 2008 - 11:13 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 6,656



Thanked: 613 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well one problem I see is with the line...

CODE

// This is the old line
<img src="download2.php?id=<?php echo $id ?>" alt="Images" />

// This is what it should be
<img src="download2.php?id=<?php echo "$id"; ?>" alt="Images" />


Notice we wrapped the $id in double quotes so it is evaluated and then make sure to end with a semicolon.

After making this change, view the page source and see if indeed the links are correctly showing the ids from your database. We want to make sure that your links are first formatted correctly.

Next I am not sure if download2.php is another file or if you are attempting to call itself. I would probably recommend that you move all your header functions and readfile into another file and call it from the first file.

So your second file should look like this...

php

error_reporting(E_ALL);
if(isset($_GET['id']))
{

include 'library/config.php';
include 'library/opendb.php';

$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM upload2 WHERE id = '$id' ";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);

// Notice here we use basename to get just the file name, not entire path
header('Content-Disposition: attachment; filename='.basename($filePath));

// Then we use the filesize to get the actual size of our file (using the full path)
header('Content-Length: ' . filesize($filePath));

// Here we use a content type of application/octet-stream because that is what images are
header('Content-Type: application/octet-stream');

// Here are some parts you were missing, one to clean the buffer and the other to flush it out.
ob_clean();
flush();

// Then read our file out to output
readfile($filePath);

include 'library/closedb.php';
exit;
}



Now that you got that part setup, you should be able to pass it ids and it will serve you up the files as is using a popup download window.

Going back to your links... you attempt to call this file downloader through the img src... this would work if PHP was writing to the output and it was not setup as an attachment style like you had in your code. But if you want this to now work as we have it here, you would simply create a HTML link which would then call download2.php and pass it the id and suddenly it would present the image download popup. As for displaying the image on the first page, you can use the img tag with the fullpath from the database to show the file and the other variables as alt tag information or part of the link text.

It is up to you. Hopefully this gives you some ideas of how this can be done and how you can make an image gallery style script where then people could click images and download them.

Enjoy!

"At DIC we be image downloading code ninjas... we also upload naked pictures of ourselves because we are sexyyyyyy" decap.gif
User is offlineProfile CardPM
+Quote Post

Ghost rider
RE: How To Display Images Uploaded In Directory
1 Dec, 2008 - 12:46 PM
Post #3

D.I.C Head
**

Joined: 29 Oct, 2008
Posts: 59


Thanks for your time and I appreciated for your explanation. I just need little bit your help to sort out this trouble.

Firstly, download2.php is calling itself.

second, all that you describe above it's doing with this line of code.
CODE
<a href="download2.php?id=<?php echo $id; ?>"><?php echo $name; ?></a> <br>

But the trouble is when it downloaded image, I can't view the image when I trying to open it. it seems that it can't find the source of image in directory. I find strange if I downloaded the image i should be able to view it!

Third, please could you just illustrate in code how to display the images(gallery) that uploaded in my directory and info of images in databases together.
Thanks so much in advance


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 12:47PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month