PHP File Upload 即使语句不正确也上传文件

PHP File Upload upload the file even if statement is not correct

HTML:

<form action="" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="password" name="filepassword" id="filepassword">
    <input type="submit" value="Upload File" name="submit">
</form>

PHP:

<?php 
if(isset($_POST["submit"])) {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $valueOne =trim($_POST["filepassword"]);
    if($valueOne != "1212"){
        move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
        echo "file uploaded successfully !";
    }
    else{
        echo "file is not entered !";
    }
}
?>

无论 if(check) 语句中的代码是什么,文件都会上传

重写你的代码如下:-

if(!empty($_POST["submit"]) && !empty($_POST["filepassword"]) && !empty($_FILES['fileToUpload'])){
    $valueOne =trim($_POST["filepassword"]);
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    if($valueOne == "1212"){ // if password is 1212 then file will upload.
    // make sure you want to check == or !=
          move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
          echo "file uploaded successfully !";
    }
    else{
          echo "file is not entered !";
    }   
}

只需交换 if 和 else 中的代码

if($valueOne != "1212"){
    echo "file is not entered !";
     }
     else{  
     move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
     echo "file uploaded successfully !";
     }