dongtun3328 2012-04-29 22:17
浏览 95
已采纳

严重的fileinfo问题

I am on shared hosting, and have been battling with them to get fileinfo working, we have finally got it working, but again I have been hit with another barrier, I have essentially got this because I am creating a new file upload tool and we need to know what mimetypes are being uploaded, but fileinfo does not want to play ball.

So, a .sql file will return text/plain, correct.

However, every other file will simply return application/octet-stream, what I am wondering is why this is the case, I don't want to be pissing the host off with tons of questions so I want to get some research on the issue before I hassle them furthur.

Code:

function get_mime($filename)
{
    $result = new finfo(FILEINFO_MIME_TYPE, "/usr/share/file/magic.mime"); 
    return $result->file($filename, FILEINFO_MIME_TYPE);
}

echo $user->get_mime($_FILES['file']['tmp_name'][$i]);

Any help would be greatly appreciated, many thanks

Update

So I have gone about updating the code of the MIMETYPE check, and I have updated the code to reflect the following(for the function):

function get_mime($filename)
{
    $result = new finfo(FILEINFO_MIME_TYPE, "/usr/share/file/magic.mime"); // return mime type ala mimetype extension

    if (is_resource($result) === true)
    {
        return $result->file($filename, FILEINFO_MIME_TYPE);
    } else {
        return "failed";
    }

    return false;
}

Obviously, as you can see I have made it output failed when it, fails. This is happening 100% of the time, and removing that if statement causes it to return application/octet-stream, which is incorrect.

The upload process will be: User uploads -> Files are moved to a temporary folder above public, (upload_check) where they will be checked by fileinfo, if the file is not what we accept it will be discarded, else it will be copied to the public files and and then discarded from the temporary folder.

I have created that process, and still fileinfo doesn't want to co-operate and returns application/octet-stream for everything, even though they are on the server just saved above the public_html folder.

Any advice?

Jake

  • 写回答

2条回答 默认 最新

  • dream752614590 2012-04-29 22:43
    关注

    Advice

    I really think you are getting things wrong here ...

    A. when a file is uploaded it also returns mime type automatically in the $_FILES

    echo $_FILES['file']['type'][$i] ;
    

    B if you want to get mime for a file that was not uploaded, or try and make a better determination that what the client headers provided, use mime_content_type http://php.net/manual/en/function.mime-content-type.php

    echo mime_content_type($fileName);
    

    C. Depreciation

    Both mime_content_type & Fileinfo are not properly maintained and they have been depreciated.

    Update: It once was incorrectly marked as deprecated in the manual, but it has been fixed https://bugs.php.net/bug.php?id=71367 on the 14th of January 2016. As can see supports 7.0 so not discontinued at all.

    Alternatively can suggest you look at the following links for replacement

    Getting mime type of zipped files

    http://www.php.net/manual/en/function.mime-content-type.php#87856

    D. You can also use getID3 http://sourceforge.net/projects/getid3/files/getID3%28%29%201.x/1.9.3/ which can check the real mime type for some common files ...

    E. Using Linux Command line probably saver

    $mime = shell_exec("file -bi " . escapeshellarg($file));   
    

    Or on Mac

    $mime = shell_exec("file -b --mime ".escapeshellarg($file));
    

    Finally

    Why do you want to work on $_FILES['file']['tmp_name'][$i] directly ?? This has been know to give so many issues

    try

    $tempDir = ""; // Temp
    foreach ( $_FILES ["file"] ["error"] as $key => $error ) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES ["file"] ["tmp_name"] [$key];
            $name = $_FILES ["file"] ["name"] [$key];
            $tempFile = "$tempDir/$name";
            move_uploaded_file ( $tmp_name, $tempFile );
            echo $user->get_mime ( $tempFile );
            unlink ( $tempFile );
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加