I'm creating an upload section that limits users to upload the following file types:


When getting the type for .pdf files, it comes up as:

and for .rar files it comes up as:

If I do a check that says:

if ($type=="application/force-download" || $type=="application/x-download")

then will users only be able to upload .PDF and .RAR files? I'm assuming these two types will allow other formats as well.

If this is the case, how can I check the extension of the file before the user is able to upload it. I was thinking of somehow capturing the last 3 letters of the filename, and then doing the check that way, except I'm not sure how to go about this. Any insight appreciated!

7 Years
Discussion Span
Last Post by ApocDen

you can you the strrpos() and substr() function to get the extension.

$ext = substr($fileName, strrpos($fileName, '.') + 1);

If your file name is Denis.pdf the $ext will be "pdf".

you can then create an array of all the extensions you allow like

$extensions = array("pdf","doc","rar","zip" ,"jpeg","gif","png");

so you can check for it like this

if (!in_array($ext, $extensions)) {
       echo "$ext is an invalid file format" ;

change names to suit your program. tell me of any errors

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.