Click to See Complete Forum and Search --> : Downloading an Image File...
themediachick
07-10-2005, 04:23 PM
Hi all,
I've been going around in circles with this - I'm just starting to learn about PHP. I get the 'gist' of what I'm trying to do, but something's not clicking for me. I understand that I'll have a .php file with code, and then in the .html file I'll have a code used for each download.
I'm creating a page that will have several images that visitors can download. They're large, so I'd like to have them click on the link and the "Save As" window be prompted to open.
Please, I beg of you, do not give me a link to another thread, lol! I've read so many of them here and on other boards, but I keep getting confused because they're either for a different file type or something doesn't work and other questions enter the thread. :(
Can someone please show me how to use php to make this happen before my head explodes?
Thank you!
:o
SpectreReturns
07-10-2005, 04:57 PM
To download the pictures, you will need an intermediate .php file to work as a downloader. The Save As window only appears if your browser does not know how to handle the supplied MIME type, so your php file needs to fake a MIME type, but provide your pictures. Here's how I see it working:
Html File
<a href="download.php?id=foo.png"><img src="images/foo-thumb.png" alt=""></a><br>
Click on the thumbnail to download the full picture.
PHP File
<?php
// Make sure that the id query is sent, and that the file exists
if (!isset($_GET['id']) || !file_exists('images/'.$_GET['id'])) {
die('No valid image was supplied.');
}
header('Content-type: image/png');
header('Content-Disposition: attachment; filename="'.basename($_GET['id']).'"'); // tell the browser how to handle the file
echo implode(null, file('images/'.$_GET['id'])); // provide the image
?>
Now, the HTML file will display the thumbnail image (images/foo-thumb.png), and link to the php file, sending the file it wants you to download (foo.png). When clicked, the PHP file checks if the file requested exists, and if it does it sends the file. Instead of using the fake MIME type, which doesn't always work, we send the propper MIME type, and a 'Content-Disposition' header, telling the browser to download the file, and what to name it.
Since the id doesn't contain the 'images/' part of the location, the .php automatically puts it in.
Hope this helps.
themediachick
07-10-2005, 05:36 PM
Thanks for your help, Spectre!
Forgive my mushy mind...I've done one too many circles with this and I'm trying to detangle the mess of codes in my head. Where do I insert the actual file names of the downloads?
Thanks...
SpectreReturns
07-10-2005, 09:17 PM
<a href="download.php?id=foo.png"><img src="images/foo-thumb.png" alt=""></a>
Where bold is the file you want to download (sans path) and boldunderline is the thumbnail of the picture (not required).
Remember, if you don't want to use the path 'images/', you will need to change both references in the .php, and if you use a file which isn't a png, you will need to change the MIME type accordingly.
[EDIT] If you mean what to call the files being downloaded, change the value of the Content-disposition.
themediachick
07-10-2005, 10:39 PM
THANK YOU!!!
:D
You are so awesome, lol! I was close to getting it, but your script looked different from the others I'd seen and it threw me off.......yours is better!
Thank you so much, Spectre! It works exactly as I wanted it to!
...and here's a huge hug for saving what was left of my sanity... :o
SpectreReturns
07-11-2005, 04:36 PM
Not a problem. Glad I could help. :D