1

I have a HTML form for entering a file and the php validates if the file entered is a xls file through the extension .xls !! Now If a user renames a image or script file say abc.jpeg to abc.xsl the file wont be validated by the php code ! How can i validate if its truly a xls file irrespective of the extension? Php code for validation

if(in_array($file_ext,$expensions)=== false)
{
         echo "extension not allowed, please choose a .xls file.";
     exit();
    }
slash
  • 69
  • 1
  • 8
  • If you are expecting like that, you have to read the file based on the format then you have to validate. Means if xls file then check the content,if its an image check the pixel, if the doc file get the content of it and validate. – Jagadeesh Jun 24 '16 at 07:46
  • http://stackoverflow.com/questions/23287341/how-to-get-mime-type-of-a-file-in-php-5-5? – Mister Henson Jun 24 '16 at 07:54
  • @MisterHenson Thanks that link helped a lot – slash Jun 24 '16 at 09:34

1 Answers1

1

Instead of using file extension, I would suggest you to use mime_content_type that Returns the MIME content type for a file. Check link http://php.net/manual/en/function.mime-content-type.php for more info.

You can prepare an array having all the allowed MIME type/ file type then check MIME content type of your uploaded file using function mime_content_type.

echo mime_content_type('test.php'); // returns text/plain

This will help your code to validate file extension/file type in cross platform (Other OS compatibility) issue.

Mangesh Sathe
  • 1,987
  • 4
  • 21
  • 40