Starx
Starx

Reputation: 78941

How to know the file type of the file which you are forcing the user to download?

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

Upvotes: 1

Views: 132

Answers (5)

Chris
Chris

Reputation: 10435

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.

Upvotes: 0

Starx
Starx

Reputation: 78941

Ok, I did it myself.

I used

$type = filetype($file); //to know the type

Upvotes: 0

Pekka
Pekka

Reputation: 449385

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.

Upvotes: 2

Ladislav
Ladislav

Reputation: 7283

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

Upvotes: 0

cjk
cjk

Reputation: 46415

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.

Upvotes: 5

Related Questions