如何使用finfo()过滤PDF文件以进行上传?

时间:2016-08-09 21:32:49

标签: php

我遇到了finfo的问题,不知道我错过了哪一步。

另外,在旁注中,

  

$ message = $ upload_errors [$ error];

如果我把错误的上传目录用于测试,

实际上不会显示错误。其他一切都很好。

这是从表单上传的多输入文件:

if(isset($_FILES['myFile'])){
$name_array = $_FILES['myFile']['name'];
$tmp_name_array = $_FILES['myFile']['tmp_name'];
$type_array = $_FILES['myFile']['type'];
$size_array = $_FILES['myFile']['size'];
$error_array = $_FILES['myFile']['error'];

        $upload_dir = "uploads/";

        for($i = 0; $i < count($tmp_name_array); $i++){

            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mime = finfo_file($finfo, $tmp_name_array[$i]);
            $ok = false;
            switch ($mime) {
               case 'application/pdf'
                    $mimeok = true;
               default:
                   die("Unknown/not permitted file type");
            }

            // 1 megabyte is 1,048,576 bytes. 5 megabyte limit 5242880
            if ($size_array[$i] > 5242880) {
                echo "Error: Document size exceeds maximum limit of 5 MB.  Please reduce the file size and retry upload";
            }else{
                if ($mimeok = false) {  
                                echo "Not correct file type<br>";
                } else{

                    if (move_uploaded_file($tmp_name_array[$i], $upload_dir.$name_array[$i])){
                        echo $name_array[$i]." upload is complete<br>";

                    } else {

                    $error = $_FILES['file_upload']['error'];
                    $message = $upload_errors[$error];
                    echo $message;
                    }

                }
            }
        }
}

1 个答案:

答案 0 :(得分:1)

首先,您正在初始化$ok,但对于您正在使用的其他脚本$mimeok

但主要问题是因为你正在进行作业而不是检查你的if:

if ($mimeok = false)
            ^ should be == or even ===