2 Replies - 10379 Views - Last Post: 01 December 2008 - 01:46 PM Rate Topic: -----

#1 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

How to display images uploaded in directory

Posted 01 December 2008 - 06:44 AM

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.

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

Is This A Good Question/Topic? 0
  • +

Replies To: How to display images uploaded in directory

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: How to display images uploaded in directory

Posted 01 December 2008 - 12:13 PM

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

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

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" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 Ghost rider  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 158
  • Joined: 29-October 08

Re: How to display images uploaded in directory

Posted 01 December 2008 - 01:46 PM

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.
<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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1