简体   繁体   English

php <5.3的替代finfo()

[英]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 它最近引起了我的注意,这里没有任何错误,它只是不起作用,因为我的服务器PHP只有5.2 lemme知道如果你们能找到一种方法使它使用MIME工作

pecl install fileinfo ? pecl install fileinfo

http://pecl.php.net/package/Fileinfo http://pecl.php.net/package/Fileinfo

Note: I know this does not directly answer the question regarding the PHP version. 注意:我知道这并没有直接回答有关PHP版本的问题。 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. 在尝试验证MP3文件时,我最近也在努力使用Fileinfo库。 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. 我已经明白Fileinfo和MP3文件存在一些已知 问题 ,即使你已经为你的环境正确设置了魔术数据库文件。

If Fileinfo cannot determine the mime type of an MP3, it may return application/octet-stream instead. 如果Fileinfo无法确定MP3的mime类型,则可能会返回application/octet-stream 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 . 这与@ mario的建议非常相似, 到目前为止似乎比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. 我在Ubuntu 10.04和OSX Mountain Lion上测试了这个,所以我猜它适用于大多数Unix环境。 I believe there are some Windows ports as well. 我相信也有一些Windows端口。

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. 说实话,我不能完全肯定此方法的安全或可靠的,但我已经看到了它推荐一个 这里 #2。 If anyone has any more information, please do share! 如果有人有任何更多信息,请分享!

On Linux servers you can be lazy and use: 在Linux服务器上,您可能会懒惰并使用:

 $type = exec("file -iL " . escapeshellcmd($fn) . " 2>/dev/null");
 $type = trim(strtok(substr(strrchr($type, ":"), 1), ";"));

mime_content_type might still work for you. mime_content_type可能仍然适合您。 While it's now under the fileinfo section in the manual, it existed way before fileinfo was brought into the PHP core. 虽然它现在是下fileinfo手册中的部分,它的存在方式之前fileinfo被带进了PHP核心。

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. 请注意,如果您的主机将Apache的mime.types文件移出正常位置,则可能需要进行一些配置,如该页面上的注释中所述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM