I've read something about
finfo() but I didn't understand!
Also I tried these:
if ($_FILES["uploadedFile"]["type"] == "text/docx"/*or == document/docx*/) echo "1";
It is obviously that it won't work. When I
echo $_FILES['uploadedFile']['type']; then I saw this for a word document(My server is Linux):
So must I use: ?
if ($_FILES["uploadedFile"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
What is an easy and reliable way to find file extension(Not only Images)? (I mean using some variables which are not filled on client side)
If you are using php 5.3, you can get true mime-type:
$finfo = finfo_open();
$file = $_FILES["uploadedFile"];
finfo_file($finfo, $file, FILEINFO_MIME_TYPE);
$file = $_FILES['uploadedFile'['name'];
$pathparts = pathinfo($file);
$ext = $pathparts['extension'];
This will give you the actual file extension to work with.
The best way to determine the file type is by examining the first few bytes of a file – referred to as “magic bytes”. Magic bytes are essentially signatures that vary in length between 2 to 40 bytes in the file headers, or at the end of a file.
your best bet is PECL extension called Fileinfo.
As of PHP 5.3, Fileinfo is shipped with the main distribution and is enabled by default
// in PHP 4 :
$fhandle = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($fhandle,$file); // e.g. gives for example "image/jpeg" for a jpeg file
// in PHP 5 you can do :
$file_info = new finfo(FILEINFO_MIME); // object oriented approach!
$mime_type = $file_info->buffer(file_get_contents($file));