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: applic开发者_高级运维ation/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
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.
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.
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
Ok, I did it myself.
I used
$type = filetype($file); //to know the type
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.
精彩评论