检查图像类型和图像尺寸时出现问题

时间:2019-07-08 15:57:09

标签: javascript php html css image

我正在从管理控制台上传图像。它正在发送到imageupload.php页。代码如下。

问题是,如果我上传的图像超过3 mb,它仍然说文件类型无效。代码有什么问题?为什么总是只显示无效的文件类型警报?对于正确的图像,它工作正常。

也请对本准则进行建议的增强。如何使它更好。

<?php
include "dblink.php";
include "session.php";
if (isset($_SESSION['userbu'])) {
    if (isset($_FILES['image'])) {
        $len       = count($_FILES['image']['name']);
        $image     = $_FILES['image'];
        $img_name  = $_FILES['image']['name'][0];
        $img_size  = $_FILES['image']['size'][0];
        $tmp_dir   = $_FILES['image']['tmp_name'][0];
        $type      = $_FILES['image']['type'][0];
        $checktype = 1;
        $checksize = 1;
        for ($i = 0; $i < $len; $i++) {
            if ($_FILES['image']['type'][$i] == "image/jpeg" || $_FILES['image']['type'][$i] == "image/jpg" || $_FILES['image']['type'][$i] == "image/png") {
                if ($_FILES['image']['size'][$i] <= '3145728‬') {
                } else {
                    $checksize = 0;
                    break;
                }
            } else {
                $checktype = 0;
                break;
            }
        }
        if ($checksize == 1 && $checktype == 1) {
            for ($i = 0; $i < $len; $i++) {
                move_uploaded_file($_FILES['image']['tmp_name'][$i], "imagesbu/" . $_FILES['image']['name'][$i]);
            }
            echo '<script language="javascript">';
            echo 'if(alert("Successfully Added")){} ';
            echo 'else{window.location = "carousel.php"}';
            echo '</script>';
        } elseif ($checksize == 0) {
            echo '<script language="javascript">';
            echo 'if(alert("Invalid file size")){} ';
            echo 'else{window.location = "carousel.php"}';
            echo '</script>';
        } elseif ($checktype == 0) {
            echo '<script language="javascript">';
            echo 'if(alert("Invalid file type")){} ';
            echo 'else{window.location = "carousel.php"}';
            echo '</script>';
        } else {
            echo '<script language="javascript">';
            echo 'if(alert("Invalid file type/size")){} ';
            echo 'else{window.location = "carousel.php"}';
            echo '</script>';
        }
    } else {
        header("location:dashboard.php");
    }
} else {
    header("location:adminlogin.html");
}
?> 

1 个答案:

答案 0 :(得分:1)

关于if/else,如果满足任何最高条件,则程序将不再寻找其他elseif。因此,您可以这样做来解决您的问题:

if($checksize==1 && $checktype==1)
{
    for($i=0;$i<$len;$i++)
    {
        move_uploaded_file($_FILES['image']['tmp_name'][$i],"imagesbu/".$_FILES['image']['name'][$i]);
    }
    echo '<script language="javascript">';
    echo 'if(alert("Successfully Added")){} ';
    echo 'else{window.location = "carousel.php"}';
    echo '</script>';
}
else{
    if($checksize==0)
    {
        echo '<script language="javascript">';
        echo 'if(alert("Invalid file size")){} ';
        echo 'else{window.location = "carousel.php"}';
        echo '</script>';  
    }
    if($checktype==0)
    {
        echo '<script language="javascript">';
        echo 'if(alert("Invalid file type")){} ';
        echo 'else{window.location = "carousel.php"}';
        echo '</script>';
    }
}

关于优化代码,您可以避免多次编写相同的内容,例如:

if($checksize==1 && $checktype==1)
{
    for($i=0;$i<$len;$i++)
    {
        move_uploaded_file($_FILES['image']['tmp_name'][$i],"imagesbu/".$_FILES['image']['name'][$i]);
    }
    $msg = "Uploaded successfully";
}
else{
    if($checksize==0 && $checktype==0)
    {
        $msg = "Invalid file type/size";
    }
    elseif($checktype==0)
    {
        $msg = "Invalid file type";
    }else{
        $msg = "Invalid file size";
    }
}
echo '<script language="javascript">';
echo 'alert("'.$msg.'");';
echo 'window.location = "carousel.php";';
echo '</script>';