Jump to content

how to check/verify file types?


j.g.

Recommended Posts

Hello List!

 

I'm working on some file uploading code, and need to check the file type and then verify that it's either ".doc, .txt, .pdf, or .xls" -- if it's not one of those, display an error and not upload it.  But, I'm not sure how to do this; I think I want to implement $_FILES[my_file_name]["type"] somehow, but I just don't know where to begin or how to implement it???

 

Would someone out there be able to help me get over this hump that I've been stuck on?

 

Here's my file upload code:

 

elseif (preg_match("/upload/i", $submit))  // $submit == "Save";
{

    if ($_FILES["glossary_file"]["tmp_name"] )
    {
       if ( move_uploaded_file($_FILES["glossary_file"]["tmp_name"], $uploadpath.$_FILES["glossary_file"]["name"]) )
       {
    $error_msg = "Successfully Uploaded Glossary File";

}
else
{
    $error_msg = "Error Uploading Glossary File";
        }
    }
    else
   {
$error_msg = "Error Uploading Glossary File";	   	
    }
}

 

Thanks in advance for your help and expertise!

-j.g.

Link to comment
https://forums.phpfreaks.com/topic/65489-how-to-checkverify-file-types/
Share on other sites

Never, ever, under any circumstances use the type field provided in the $_FILES array; it is set by the browser and not PHP or the web server.  This means the value can be easily faked.

 

Refer to this:

http://www.php.net/manual/en/ref.fileinfo.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.