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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里