PHP in_array问题

时间:2018-05-23 07:39:11

标签: php

所以我正在尝试上传图片,我只想上传jpg,jpeg和png,所以我有这个文件检查图像是jpg,jpeg还是png:

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    foreach ($_FILES['img_items'] as $key => $image) {
        var_dump($image);
    }

    $filetype = $_FILES['img_items']['type'];

    $allowed = array("png" => "image/png", "jpg" => "image/jpg", "jpeg" => "image/jpeg");
    if(in_array($filetype, $allowed)){
        echo 'PASSED<br/>';
    }else{
        echo 'FAILED<br/>';
    }

    var_dump($filetype);

}

?>

上面的代码是为了测试目的而简化的。 如果文件类型是jpg,jpeg或png,它应该给我文件详细信息然后回显“PASSED”。

当我上传jpg或jpeg图像时,它可以正常使用“PASSED”作为结果。 但是当我上传PNG文件时,我得到了“FAILED”..这没有意义,我甚至切换了数组的顺序,以便png在前面,但仍然是同样的问题..

我用3种不同的PNG图像测试了这个,由不同的相机/光源拍摄。

这是它给我看的内容: https://ibb.co/bVNkGT

原始文件是:

# Check for packaging image and process it.
            if(isset($_FILES["img_package"]) && $_FILES["img_package"]["error"] == 0){
                $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "png" => "image/png");
                $filename = $_FILES["img_package"]["name"];
                $filetype = $_FILES["img_package"]["type"];
                $filesize = $_FILES["img_package"]["size"];

                $ext = pathinfo($filename, PATHINFO_EXTENSION);
                if(!array_key_exists($ext, $allowed)){
                    $_SESSION['errMsg'] = 'Uploaded photo extension is not allowed! Only .jpg, .jpeg and .png is allowed';
                    header("location: /inbound/add");
                };

                $maxsize = 3 * 1024 * 1024;
                if($filesize > $maxsize){
                    $_SESSION['errMsg'] = 'Uploaded photo exceeded 3MB file limit';
                    header("location: /inbound/add");
                };

                if(in_array($filetype, $allowed)){

                    $rand = rand(pow(10, 6-1), pow(10, 6)-1);

                    if(file_exists("../images/". $rand . $_FILES["img_package"]["name"])){
                        $_SESSION['errMsg'] =  $rand . $_FILES["img_package"]["name"] . " exsisted!<br/>Try again if the photo is diffrent.";
                        header("location: /inbound/add");
                    }else{
                        # Package Image Mover is moved below so thefile will NOT be moved untill everyhting is cleared.
                    } 
                }else{
                    $_SESSION['errMsg'] = 'Uploaded photo extension is not allowed! Only .jpg, .jpeg and .png are allowed 1';
                    header("location: /inbound/add");
                }
            }else{
                $_SESSION['errMsg'] = "Error: " . $_FILES["img_package"]["error"];
                header("location: /inbound/add");
            }

            # Check for items image and process it.
            if(isset($_FILES['img_items'])){

                # Define variables
                $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "png" => "image/png");
                $maxsize = 3 * 1024 * 1024;

                # Loop the image array
                foreach ($_FILES["img_items"]["error"] as $key => $error){
                    # Check for error
                    if($error == UPLOAD_ERR_OK){
                        # Define variable
                        $filename = $_FILES["img_items"]["name"];
                        $filetype = $_FILES["img_items"]["type"];
                        $filesize = $_FILES["img_items"]["size"];
                        # Check if the file extenstion is allowed
                        $ext = pathinfo($filename, PATHINFO_EXTENSION);
                        if(!array_key_exists($ext, $allowed)){
                            $_SESSION['errMsg'] = 'Uploaded photo extension is not allowed! Only .jpg, .jpeg and .png is allowed';
                            header("location: /inbound/add");
                        };
                        # Check file size
                        if($filesize > $maxsize){
                            $_SESSION['errMsg'] = 'Uploaded photo exceeded 3MB file limit';
                            header("location: /inbound/add");
                        };
                        # Confirm file is allowed
                        if(in_array($filetype, $allowed)){
                            # Randomize prefix to prevent duplicate image error
                            $rand = rand(pow(10, 6-1), pow(10, 6)-1);
                            # Check if the file exsisted
                            if(file_exists("../images/". $rand . $_FILES["img_items"]["name"])){
                                $_SESSION['errMsg'] =  $rand . $_FILES["img_items"]["name"] . " exsisted!<br/>Try again if the photo is diffrent.";
                                header("location: /inbound/add");
                            }else{
                                define ('SITE_ROOT', realpath(dirname(__FILE__)));
                                move_uploaded_file($_FILES["img_items"]["tmp_name"], SITE_ROOT."inferno/inbound/images/" . $rand . $_FILES["img_items"]["name"]);
                                $img_items[] = $rand.$_FILES["img_items"]["name"];
                            } 
                        }else{
                            $_SESSION['errMsg'] = 'Uploaded photo extension is not allowed! Only .jpg, .jpeg and .png are allowed 2';
                            header("location: /inbound/add");
                        } 
                    }else{
                        $_SESSION['errMsg'] = "Error: " . $_FILES["img_items"]["error"];
                        header("location: /inbound/add");
                    }
                }
            }

第一个(img_package)工作正常但第二个(img_items)在给定PNG文件时返回错误。 任何帮助都是适当的。提前谢谢。

3 个答案:

答案 0 :(得分:0)

尝试使用代码:

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    foreach ($_FILES['img_items'] as $key => $image) {
        var_dump($image);
    }

    $filetype = $_FILES['img_items']['type'];

    $allowed = array("png" => "image/png", "jpg" => "image/jpg", "jpeg" => "image/jpeg");
    if(in_array($filetype[0], array_values($allowed))){
        echo 'PASSED<br/>';
    }else{
        echo 'FAILED<br/>';
    }

    var_dump($filetype);

}

?>

答案 1 :(得分:0)

由于我尝试上传多个图片,因此$ filetype变量实际上是一个数组,在上传非JPG或JPEG文件时会以某种方式导致错误。

修复是:

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $filetype = $_FILES['img_items']['type'];
    $allowed = array("png" => "image/png", "jpg" => "image/jpg", "jpeg" => "image/jpeg");

    foreach ($filetype as $key => $type) {

        var_dump($type);

        if(in_array($type, $allowed)){
            echo 'PASSED<br/>';
        }else{
            echo 'FAILED<br/>';
        }

    }

}

?>

现在它按预期工作了。 对不起造成的混乱/麻烦。

答案 2 :(得分:-1)

尝试这样做

        $fileType = $_FILES['file']['name'];

        $fileExt = explode('.', $fileType);
        $fileActualExt = strtolower(end($fileExt));

        $allowed = array('jpg', 'jpeg', 'png');

        if(in_array($fileActualExt, $allowed)){


                     }