alternative to finfo() for php < 5.3
<?php
$finfo = new finfo();
$fileinfo = $finfo->file($_FILES["fileToUpload"]["tmp_开发者_运维百科name"], FILEINFO_MIME);
switch($fileinfo) {
case "image/gif":
case "image/jpeg":
case "image/png":
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
"upload/" . $_FILES["fileToUpload"]["name"]);
echo "Your file has successfully been uploaded, and is awaiting moderator approval for points." . "<html><br><a href='uploadfile.php'>Upload more.</a>";
break;
default:
echo "Files must be either JPEG, GIF, or PNG and less than 10,000 kb";
break;
}
?>
it has recently been brought to my attention there is nothing wrong here, it just doesnt work because my servers php is only at 5.2 lemme know if you guys can find a way to make it work using MIME
pecl install fileinfo
?
http://pecl.php.net/package/Fileinfo
On Linux servers you can be lazy and use:
$type = exec("file -iL " . escapeshellcmd($fn) . " 2>/dev/null");
$type = trim(strtok(substr(strrchr($type, ":"), 1), ";"));
mime_content_type
might still work for you. While it's now under the fileinfo
section in the manual, it existed way before fileinfo
was brought into the PHP core.
Do note that it might require a bit of configuration if your host moved Apache's mime.types
file out of the normal location, as documented in the comments on that page.
Note: I know this does not directly answer the question regarding the PHP version. However, I found this post while trying to solve my issue, and so it may be helpful for someone in the future.
I too have been struggling with the Fileinfo
library recently while trying to validate MP3 files. I've come to understand that there are some known issues with Fileinfo
and MP3 files, even if you've properly set the magic database file for your environment.
If Fileinfo
cannot determine the mime type of an MP3, it may return application/octet-stream
instead. Not very helpful when trying to validate a file.
As an alternative, I've started using the following system command. This is very similar to @mario’s suggestion, and so far seems more reliable than Fileinfo
.
$path = 'path/to/your/mp3/file.mp3';
$mime = exec('file -b --mime-type ' . $path);
I've tested this on Ubuntu 10.04 and OSX Mountain Lion, so I'm guessing it works on most Unix environments. I believe there are some Windows ports as well.
Truth be told I'm not entirely sure how safe or reliable this method is, but I've seen it recommended a few times here on Stackoverflow. If anyone has any more information, please do share!
精彩评论