Reputation: 1892
I've read something about MIME
and 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):
application/vnd.openxmlformats-officedocument.wordprocessingml.document
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)
Upvotes: 0
Views: 206
Reputation: 39638
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));
Upvotes: 0
Reputation: 1035
How about
$file = $_FILES['uploadedFile'['name'];
$pathparts = pathinfo($file);
$ext = $pathparts['extension'];
This will give you the actual file extension to work with.
Upvotes: 2
Reputation: 382676
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);
finfo_close($finfo);
More Info:
Upvotes: 3