idexo
idexo

Reputation: 63

Nested if(isset) is not working as intended

I have a nested if (isset) but it doesn't seem to work as I intended. I also tried if (!empty). Please see the code below.

if (isset($_POST['submit'])){
   if(isset($_FILES['userFile'])){
     //upload file, make tumbnail, then put info to the the tables on the database
     //header location
   } else if (isset($_POST['comment'])){
     //put info to the tables on the database
     //header location

   } else {
     die("you didn't write a comment or upload a file");
   }
}

My intention was to let user upload a pic without a comment or let him or her comment without uploading a picture and obviously not letting the user to send an empty form. I searched a bit but I didn't find much info about nested isset or empty.

Am I on the right track? if so; how can I make this one work?

Because when I try the code above without setting a userFile, it still gives me the error from the userFile part and the part is like below:

if($imagesize2 > $max_size2) {
    die("your file is bigger than max size");
} else if($safe_imagetype2 =='image/jpeg' || $safe_imagetype2 =='image/png' || $safe_imagetype2 == 'image/jpg' || $safe_imagetype2 == 'image/gif') {
    move_uploaded_file($safe_uploadTmp2, "./images/$safe_uploadName2");
} else {
    die("it must be gif, jpg or png");
}

any help appreciated.

Upvotes: 0

Views: 326

Answers (2)

UpVs
UpVs

Reputation: 1958

You should to check the error during the file upload this way:

if (isset($_FILES['userFile']) && UPLOAD_ERR_OK == $_FILES['userFile']['error']) {
    // The file has been uploaded successfully
}

Upvotes: 2

9997
9997

Reputation: 1357

This is what you're looking for.

if(isset($_FILES['userFile']) && strlen($_FILES['userFile']['inputNAME']) > 1)

Upvotes: 2

Related Questions