I've been reading the forum for past couple of hours, trying to
find the solution for my problem. I am having problem with
isset() function.

Here is my code. Problem is that it always returns 'file ok'
even though there is no file selected. I tried both isset() and
!isset(), but both give me the same result.
Hope someone can help me.

if($_POST['submit_01']){ 
   if(!isset($_FILES['tab_1_photo'])){
         echo 'no file';
    }else{
        echo 'file ok';
    }
}

And this is a part of my form.

<form action="'. $_SERVER['PHP_SELF'] .'" method="post" id="edit" name="edit" enctype="multipart/form-data">
<input type="file" name="tab_1_photo" >
<input type="submit" value="Preview" name="submit_01">

    Hi,

    the $_FILES array is set even if no file was submitted. In order to check if a file has been submitted or not you can use the following code (the error value of the array will be 4 in that case):

    if(isset($_POST['submit_01'])){ 
       if(isset($_FILES['tab_1_photo']) && ($_FILES['error'] == 4)){
             echo 'no file';
        }else{
            echo 'file ok';
        }
    }
    

    Thomas

      Hi, Thanks for your reply to both of you.

      I tried both empty() and what Thomas said, but either one didn't work fo me.... 🙁

      Question to Thomas,

      Am I supposed to create a new file input tag and put value=4? or value 4 is already set as "error"? I am little confused.
      I tried without creating new file input tag. I just replaced my code to your code, but didn't work.

      What am I doing wrong?

        To test how the array looks like add

        print_r($_FILES);

        on top of your script to check how the array looks like. Please post the results (without submitting an image).

        Thomas

          Thomas

          I didn't select any file, and click "send".

          This is the result I got.

          Array ( [tab_1_photo] => Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 ) )

          file ok

            What exactly are you trying to check? isset() checks to see if the variable is set. If the form control 'tab_1_photo' exists, then isset() will return true (if the check is run after the form is submitted).

              What I am trying to do is that whether the user has selected a image file to upload or not when he submit the form.

              No matter what i do, it returns 'file ok' even though the user didn't select any file and submit the form.

              The last post was the result.

              I didn't select any file and hit submit. As you can see, error=4 and everything else is empty, but still returns 'file ok' as if there is some file selected.

              I really don't know what to do...

                Checking whether the control had anything in it worked fine using empty():

                <?php
                   if($_POST) {
                      if(empty($_POST['file_1_photo'])) {
                         echo 'Please select a file to upload.';
                      }
                      else {
                         // Further processing here
                      }
                   }
                ?>
                

                Just an example, of course (user could type in a space and get past that).

                  I just tried your code, but empty() is not working either.
                  Even though I selected a file, it returns empty.

                    Try this modified code:

                    if(isset($_POST['submit_01'])){  
                    if(isset($_FILES['tab_1_photo']) && ($_FILES['tab_1_photo']['error'] == 4)){ echo 'no file'; }else{ echo 'file ok'; } }

                    By the way ... if you want to check if the file upload was ok I'd rather do something like:

                    if(isset($_POST['submit_01'])){  
                    if(isset($_FILES['tab_1_photo']) && ($_FILES['tab_1_photo']['error'] == 0)){ echo 'file ok'; }else{ // add some code that deals with the different possible // error values. } }

                    It doesn't mean that the file upload was ok if the error value is not 4. There are some other values meaning that the file upload failed for some reason.
                    Follow this link to get an overview. Additionally, check the other values of the array like filesize to determine if something went wrong.

                    Thomas

                      Thank you soooooooooo much, Thomas.

                      I tried the first code, it really worked as it is supposed to!!!!
                      I cannot explain how much I appreciate your help, and not giving up on me...

                        Write a Reply...