首页 > 解决方案 > PHP in_array 问题

问题描述

所以我正在尝试上传图片,我只想上传 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文件时返回错误。任何帮助都会得到帮助。提前致谢。

标签: php

解决方案


尝试使用代码:

<?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);

}

?>

推荐阅读