SOLVED: Original Filenames in images from PHP

A

Anonymous

Guest
Took me ages to figure this out so here it is along with keywords for search engines...

Code:
# Tell the broswer that the file coming up is Jpeg/Jpg
header("Content-Type: image/jpeg");

# Tell the browser to assign the file a proper name isntead of the PHP file's name
header("Content-Disposition: inline; filename=\"$ImgName\"");

# Direct the browser to the image itself
header("location: $image_path");

Streaming PHP image files (jpg, jpeg, gif) with original filenames rather than PHP file's name. Uses content headers, disposition, location, content-type.
 
because you are sending a redirect ("Location: ...") header, you are still just sending the http request to the file in question. Using a location header also means you cannot access files above public root or where-ever a direct http request cannot access.
To stream the file, you'd probably do best using readfile as the last part, which does pass the content disposition to the browser properly even though the script name will still be whatever.php?ref=imagename

so

header("Content-type: image/jpeg");
header("Content-Disposition: inline; filename='New Image Name'");
readfile('image1/11.jpg');

would output the relatively linked image1/11.jpg with a src attribute of scriptname.php.
the difference would be when someone chose to save the imageby right-click-save-as - they would be given a default name of 'New Image Name'.jpg

I'm not overly certain of which parts a search engine would actually read
 
Thanks for that. Thats what I was using before, but it just took ages for the damn thing to work.

This seems to work, and right-click-save-as works too, and its pretty speedy so I have no idea whats different.

Thanks for the updated version anyhow. :D
 
Well my British buddy, it seems the code gave up the ghost (or the pootergheist!) and I'm using yours now!

Thanks man. Happy New Year.
 
Back
Top