Jump to content

Trying to use finfo() to get MIME type


limitbreaker

Recommended Posts

Hi,

Basically I'm trying to see if a file is a photo, video, or sound by detecting mime type through the finfo functions:

$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $file['tmp_name']);

Thing is, my following if statements aren't detecting the mime types properly (meaning EVERYTHING is considered a photo):

if ( $mime == ("image/jpeg" || "image/pjpeg" || "image/gif" || "image/png" || "image/x-png") ) 

elseif ( $mime == ("video/mp4" || "video/x-ms-wmv") ) 

elseif ( $mime == ("audio/mpeg" || "audio/x-wav" || "audio/x-ms-wma") ) 

else { header("Location:home.php"); } 

I'm guessing finfo is giving me problems, but I don't really know another way to distinguish these files. Is there possibly a different way I can get a little higher level of security (I know MIME types can be faked)?

 

*The statements do have properly functioning code in brakets {}, I just removed it for the sake of simplicity

 

--Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/282494-trying-to-use-finfo-to-get-mime-type/
Share on other sites

The || operator does not work like that. You have to use full conditions for each test:

if ($mime == 'image/jpeg' || $mime == 'image/pjpeg' || $mime == 'image/gif' ...)
An alternative is to use in_array with an array of possible mime types.

The || operator does not work like that. You have to use full conditions for each test:

if ($mime == 'image/jpeg' || $mime == 'image/pjpeg' || $mime == 'image/gif' ...)
An alternative is to use in_array with an array of possible mime types.

 

Alright, this distinguishes images from other media, but the only other filetype that makes it through this filter successfully is wav. Anything else just gives the upload successful message but nothing happens at all. This is the code in each if statement (only slight variations for each):

mysql_query("INSERT INTO updates (id,name,numb,music,folder) VALUES('$id', '$name', '$num', '${file['name']}', '$folder')");
move_uploaded_file($file["tmp_name"], "users/$id/$folder/${file["name"]}");

would something like $_FILES['file']['type'] work instead of this finfo()?

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.