Mime类型PDF php上传

时间:2013-01-22 03:17:21

标签: php file-upload mime-types

我有这个片段

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"]);

当我尝试上传图片时,它表明它不是允许的文件类型,当文件是PDF文档时,application/pdf是正确的mime类型吗?

2 个答案:

答案 0 :(得分:6)

在默认情况下你忘了打破:)

顺便说一下,为了回答你的直接问题,我发现自己一遍又一遍地回顾这个话题:Proper MIME media type for PDF files

答案 1 :(得分:2)

试试这个和

 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"]);

如果无法解决您的问题,请告诉我们finfo_openfinfo_file函数的外观。