May 25, 2010

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

Question by Starx

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

$file = "filethis.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

Answer by cjk

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.

Answer by Starx

Ok, I did it myself.

I used

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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!