How to allow only files with a specific extension to be downloaded


I am having trouble in coding a script. I need only a specific file type to be downloadable. I dont know how to check the file type using PHP. I have the script ready for downloading any file type, but now I need to add a filter which allows only certain file types to be downloadable which I have specified in the code.

Here is the code:

Please look at the code and advise.

Thank you.

Your paste bin code fails because you cannot put HTML in the PHP header tag, though it’s a nice try and shows that you are thinking.

The way to do it is by using the .htccess file to redefine what the webserver is supposed to with with a file.

Try this

Another thing you need to think about is what are you going to do if someone renames a file to change the extension such that the file content is not of the type identified by the extension?

Check This Out:

This maybe useful to find out file’s mimetype. It’s also very easy.