views:

42

answers:

5

I am trying to force the user to download a file. For that my script is:

$file = "file\this.zip";        
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip"); //This is what I need
header("Content-Transfer-Encoding: binary");
readfile($file);

The files I am going to upload my not be .zip all the time so I want to know the content type of the image I am going to receive in $file. How to accomplish this

+5  A: 

I always put application/octet-stream (or somwething like that) as the browser can't then try and display it in any way, it will always force the user to Save / Run, then the file type is often inferred by the extension.

ck
A: 

Hey, on the link below you can see the values of the Content-Type that you need:

Internet Media Type

There are just too many to list here Ladislav

Ladislav
... I think he meant programmatically...
Ignacio Vazquez-Abrams
+2  A: 

For images, the most reliable is getimagesize(). For any other type, the Fileinfo functions are the right thing.

I second what ck says, though: When forcing a download, octet-stream is the best choice to prevent things from opening in the browser or the respective client application.

Pekka
A: 

Ok, I did it myself.

I used

$type = filetype($file); //to know the type
Starx
That'll just return 'file' for all of your files. Not what you want.
Chris Smith
Yeah, I just noticed. I have used application/octet-stream as suggested
Starx
A: 

You need to send the mime-type there. You can get this in PHP by using the mime_content_type function. There's also the Fileinfo extension which provides a way of getting lots of file information, including the mime type.

Chris Smith