即使语句不正确,PHP文件上传也会上传文件

时间:2017-02-21 06:32:40

标签: php html5 web php-5.5

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)语句中的代码是什么,文件都上传

2 个答案:

答案 0 :(得分:1)

重写您的代码如下: -

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 !";
    }   
}

答案 1 :(得分:0)

只需在if和else

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