【问题标题】:PHP File Upload upload the file even if statement is not correctPHP File Upload 即使语句不正确也要上传文件
【发布时间】:2017-02-21 06:32:39
【问题描述】:

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

【问题讨论】:

  • 如果条件为真,你想上传还是不上传?
  • 条件为真我要上传..
  • 我认为你应该检查 $_FILES inseat POST
  • 在我看来你必须写 if($valueOne == "1212"){ 代替 if($valueOne != "1212"){

标签: php html web php-5.5


【解决方案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 !";
    }   
}

【讨论】:

  • 使用正确密码上传文件时提示文件未输入!
  • 好的。那么您应该检查 == 而不是 !=。请检查我的更新答案。
  • 明白了.. 你必须将 "$valueOne != "1212"" 更改为 "$valueOne == "1212"" ... 不能正常工作
【解决方案2】:

只需交换 if 和 else 中的代码

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

【讨论】:

    猜你喜欢
    • 2020-10-29
    • 2016-08-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2016-11-09
    • 2015-12-01
    • 2018-04-02
    相关资源
    最近更新 更多