Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5317
  • Last Modified:

PHP Content-disposition: inline?

I need to display files through a PHP script. I made me a script named files.php. I call the files as files.php?id=123213 for example.

I get the file to display, or download - regarding if it's an image or not. For images, the image is displayed, but the name of the image is "files.php", if you do a save as you get "files.php.jpg", and i want it to be "something_else.jpg".

I set header("Content-Disposition: attachment; filename='something_else.zip'"), and it works great, for files i want to download.
For images i just want them to display with proper name. Tried header("Content-Disposition: inline; filename='something_else.jpg'") - doesn't work :(

Help? :)

Thanks in advance.
2 Solutions
make the header as

Content-Disposition: attachment; filename='something_else.jpg'

you can always add one more tag to display the image with

echo "<img src='something_else.jpg'>";

to display the image
Basically to do this you'll need to use mod_rewrite in a .htaccess file or in your http.conf.

You currently access your page like www.website.com/files.php?id=123213

what you should do is something like this for images: www.website.com/123213.jpg

then in your .htaccess you'll need something like this:

RewriteEngine On
RewriteRule ^([0-9]+)\.(jpg|jpeg|png|gif)$ files.php?id=$1

You can't do it with content disposition I'm afraid.

Say you don't know it's an image beforehand you could do a header redirect first someone goes to files.php?id=123213 the top of your files.php could do this:


  if (isImageFile($_GET["id"])) { //you need to write isImageFile function yourself
    $filext = getFileExtension($_GET["id"]); // again write this function to get the image extension
    header("location: /{$_GET["id"]}.$filext");

This combined with the mod_rewrite stuff should give you what you were looking for.
edmund01Author Commented:
Thanks for the answers. None of it really worked. keteracel solution is a bit more what i was looking for, but the name same as ID is not what i want, because behind that ID theres a name saved, thats what i would need. And unless i make DB keys with that name, this won't work.

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now