Mime 类型 PDF php 上传

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14451011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:16:55  来源:igfitidea点击:

Mime Type PDF php upload

phpfile-uploadmime-types

提问by kira423

I have this snippet

我有这个片段

if ($_FILES['tax']['error'] !== UPLOAD_ERR_OK) {
    die("Upload failed with error " . $_FILES['tax']['error']);
}
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES['tax']['tmp_name']);
    $ok = false;
    switch ($mime) {
        case 'image/gif':
        case 'application/pdf':
        case 'image/png':
        $ok = true;
        default:
    die("Unknown/not permitted file type");
    }
    move_uploaded_file($_FILES["tax"]["tmp_name"],"pints/" . $_FILES["tax"]["name"]);

When I try to upload the image it states that it isn't a permitted file type, when the file is a PDF document, is application/pdfthe correct mime type?

当我尝试上传图像时,它指出它不是允许的文件类型,当文件是 PDF 文档时,是否application/pdf是正确的 MIME 类型?

回答by Lim H.

You forgot to break before the default case :)

您忘记在默认情况下中断 :)

By the way, for the sake of answering your direct question, I've found myself referring back to this thread over and over again: Proper MIME media type for PDF files

顺便说一句,为了回答您的直接问题,我发现自己一遍又一遍地参考这个线程:PDF 文件的正确 MIME 媒体类型

回答by Davit

Try this and

试试这个和

 if ($_FILES['tax']['error'] !== UPLOAD_ERR_OK)
        die("Upload failed with error " . $_FILES['tax']['error']);

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES['tax']['tmp_name']);
    $ok = false;
    switch ($mime) {
        case 'image/gif':
        break;
        case 'application/pdf':
        break;
        case 'image/png':
            $ok = true;
        break;
        default:
            die("Unknown/not permitted file type");
        break;
    }
    move_uploaded_file($_FILES["tax"]["tmp_name"],"pints/" . $_FILES["tax"]["name"]);

If won't solve your problem then tell us how do finfo_openand finfo_filefunctions look like.

如果不能解决您的问题,请告诉我们 dofinfo_openfinfo_file函数的外观。