0

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

No matter what the code inside the if(check) statement, file get uploaded

4
  • you want to upload or not if condition is true? Commented Feb 21, 2017 at 6:35
  • I want to upload if the condition is true .. Commented Feb 21, 2017 at 6:36
  • I think you should check $_FILES inseat POST Commented Feb 21, 2017 at 6:37
  • in my view you have to write if($valueOne == "1212"){ in place of if($valueOne != "1212"){ Commented Feb 21, 2017 at 6:40

2 Answers 2

1

Rewrite your code as below:-

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 !";
    }   
}
Sign up to request clarification or add additional context in comments.

4 Comments

it says file is not entered when you upload the file with correct password !
okay. then you should check == instead of !=. Please check my updated answer.
got it.. you have to change "$valueOne != "1212"" into "$valueOne == "1212"" ... not its workin g
0

Just Swap the code within if and else

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.