1

I am working on a simple document management system for a site - the user can upload around 20 different file types and the docs are renamed then stored in a folder above www, an entry is created in a docs table to capture meta data entered by the user and the item is then retrieved via another php file so the stored location for the files are hidden from the user.

When a user clicks to download a file using a simple a href it calls, for example, "view.php?doc=image.jpg" - when they do this currently the file opens in the browser so a jpg opens a window with pages of "wingdings" like characters etc.

I would like to be able to force a open/save dialogue box so the user decides what to do and my app doesn't try to open in the browser window with the above results.

From a previous posting I found I know I cannot pass the mime type in the "a href" tag so what other options do I have? Could I put header information into the below view.php file, for example?

$_file = $_GET['doc'];

$filename = './dir/'.$_file; 

if (file_exists($filename)) { 
echo file_get_contents('./dir/'.$_file);
} else { 
echo "The file $_file does not exist"; 
} 
;
bhttoan
  • 2,641
  • 5
  • 42
  • 71
  • possible duplicate of [Force download image?](http://stackoverflow.com/questions/5648967/force-download-image) – dev-null-dweller May 13 '12 at 12:05
  • I don't feel it is a duplicate as, if these were all one file type then it would be fairly simple to do - my challenge (in my head) is that the output file could be any one of 20 types so I need to have different headers depending on the file type. – bhttoan May 13 '12 at 19:22

1 Answers1

1

You could use get_headers() to get the MIME type header of the desired file, and then use header() to output those headers into the file you're showing.

Alternatively, to simply force downloads, this:

header('Content-Description: File Transfer');
header("Content-type: application/octet-stream");

Should do it.

Jake M
  • 544
  • 4
  • 13
  • I tried this but get_headers() can only be used against a URL and as this file is outside the www root I don't think this will work in this instance. – bhttoan May 13 '12 at 19:20
  • Added a solution that doesn't use `get_headers()`. dev-null-dweller was right, [Force download image](http://stackoverflow.com/questions/5648967/force-download-image) has the solution you want. I just copied-changed it over here. – Jake M May 13 '12 at 19:39