Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Dumb question about isset() and empty()

    So I was reading the PHP Manual about $_FILES and found this comment...

    In the past you could unconditionally call $_FILES['profile_pic'] without ever having to worry about PHP spitting an "Undefined index: profile_pic" error (so long as the page posting had a file input on it (e.g. <input type="file" name="profile_pic" />)). This was the case regardless of whether or not the end user actually uploaded a file. These days, with so many people browsing the web via iPads, you have to explicitly check to see if the input isset($_FILES['profile_pic']) before calling into it, else you'll get the aforementioned error message. This is because iOS devices running Safari disable file inputs thereby causing them to be treated as if they don't exist. Time to update your scripts!

    -john

    Just when I thought my "upload.php" script was done, yet another issue?!

    This person talks about using isset(), but after some thought, I am wondering if my existing code doesn't check for the same issue, but just in a different way...

    PHP Code:
        // Check for File.
        
    if (empty($_FILES['userPhoto']['tmp_name'])){
            
    // No File Exists.
            
    $errors['upload'] = 'Choose a File.';

        }else{
            
    // File Exists.
            
    $tempFile $_FILES['userPhoto']['tmp_name'];

        } 
    I think my code would handle the iPad/Safari issue this person talks about, but what do you think??

    Thanks,


    Debbie

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    isset() checks that the variable actually exists.

    EG you have a form with a field called name. If you type nothing into it and send it, it will still be set (because the browser sent it) but it will be empty. In other words, $_POST['name'] = ''; but it is still a valid variable that is set.

    Now lets suppose your form also has a javascript that adds another field if you press a button - say its a button that lets the user supply an email - click the button and a field with the name email is created and displayed.

    If you send the form without creating the email field, then $_POST['email'] is not set - it will not exist as far as php is concerned because the browser did not send it.

    See the difference? A variable is only set if the users browser sends it OR you create it in your code. empty() will just determine if the variable contains anything or not.

    Thats my take on it anyway..
    I can't really think of anything to write here now...

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Here is a snippet from my Form...
    PHP Code:
        <!-- User Photo -->
        <label for="userPhoto">Filename:</label>
        <input id="userPhoto" name="userPhoto" type="file" />
        <?php
            
    if (!empty($errors['upload'])){
                echo 
    '<span class="error">' $errors['upload'] . '</span>';
            }
        
    ?>
    And here a PHP snippet which validates the Form...
    PHP Code:
        // Check for File.
        
    if (empty($_FILES['userPhoto']['tmp_name'])){
            
    // No File Exists.
            
    $errors['upload'] = 'Choose a File.';

        }else{
            
    // File Exists.
            
    $tempFile $_FILES['userPhoto']['tmp_name'];
        } 

    If a User goes to my Upload Form, clicks on the "Browse" button, navigates to a Photo (file) and chooses it, then path to that Photo will be filled in the Filename field, and when the User clicks "Upload Photo", the $_FILES array will contain the Filename and more.

    By contrast, if a User does not do this and clicks "Upload Photo", then $_FILES is "empty" on my computer.

    So what is in $_FILES?

    I guess this guy is saying that on an iPad that $_FILES would no longer be "empty" but not even set?

    Still kinda in the fog here...


    Debbie

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    it's prolly old post from times when iOS wasn't capable of uploading files. i wouldn't worry about it too much

  • #5
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    ... but to be on a safe side you could do:
    Code:
    if (!isset($_FILES['userPhoto']['tmp_name']) || empty($_FILES['userPhoto']['tmp_name'])){
            // No File Exists.
            $errors['upload'] = 'You haven\'t selectrd file or your browser is internet challenged';
    
    }else{
            // File Exists.
            $tempFile = $_FILES['userPhoto']['tmp_name'];
    
    }
    how that site works? does it force u to upload a pic during registration process, or you do it afterwards?
    if it's not diring registration then sollution is simple. just don't show upload form to users with iOS
    Last edited by patryk; 03-24-2013 at 02:57 AM.

  • #6
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    it's prolly old post from times when iOS wasn't capable of uploading files. i wouldn't worry about it too much
    Regardless, can you help me understand if my current code would work or not?

    The commenter and Tango make it sound like "No."

    If that is the case, then do I add another layer above my empty() code and add an isset() check?


    Hey, Tango, does this look better...
    PHP Code:
            // **********************
            // Check for File. (2)    *
            // **********************
            
    if (isset($_FILES['userPhoto']['tmp_name'])){
                
    // Arrayis Set.
                
    if (empty($_FILES['userPhoto']['tmp_name'])){
                    
    // No File Exists.
                    
    $errors['upload'] = 'Choose a File.';

                }else{
                    
    // File Exists.
                    
    $tempFile $_FILES['userPhoto']['tmp_name'];

                }
    //End of CHECK FOR FILE
            
    }else{
                
    // Array Not Set.
                // Throw some error.
                
            


    Debbie

  • #7
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    if browser is making POST request and is uploading files, then $_FILES will be set,
    so you could do it like that:
    PHP Code:
            // ********************** 
            // Check for File. (2)    * 
            // ********************** 
            
    if (isset($_FILES)){ 
                
    // Arrayis Set. 
                
    if (empty($_FILES['userPhoto']['tmp_name'])){ 
                    
    // No File Exists. 
                    
    $errors['upload'] = 'Choose a File.'

                }else{ 
                    
    // File Exists. 
                    
    $tempFile $_FILES['userPhoto']['tmp_name']; 

                }
    //End of CHECK FOR FILE 
            
    }else{ 
                
    // Array Not Set. 
                // Throw some error. 
                 
            

    but why even go there? don't let old iOS to attempt to upload on a first place

  • #8
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    if browser is making POST request and is uploading files, then $_FILES will be set, so you could do it like that:
    Do others agree with this assertion?


    but why even go there? don't let old iOS to attempt to upload on a first place
    Because I have no way of knowing WHO is accessing my website or HOW they might be trying to upload a Photo...


    Debbie

  • #9
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    oh but you do.
    you just have to use some javascript on your page with form

  • #10
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    oh but you do.
    you just have to use some javascript on your page with form
    Sorry, I don't do JavaScript.

    Not an option for now.


    Debbie

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by patryk View Post
    oh but you do.
    you just have to use some javascript on your page with form
    But that is client side validation and you can't rely on that. Supposing someone disables javascript? (Not that anyone should these days as so many sites require it). You'd be stuffed.

    Debbie is correct in wanting to use server side validation and you shouldn't sway anyone from that. The one thing that you can do is to check the incoming headers (easier said than done using non apache servers though) to see what details the browser has sent about itself and the host OS. Admittedly thats going into unwanted complexity though and when you start on that you'll end up starting on your own tracking software etc which you really don't need.

    @Debbie the revised code you showed above should do it. It checks that the $_FILES field is set and that it is not empty. That would also be the way I would do it. You could also use array_key_exists() to make sure that the correct form field exists in the $_FILES array if you want
    I can't really think of anything to write here now...

  • #12
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,127
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    @Debbie the revised code you showed above should do it. It checks that the $_FILES field is set and that it is not empty. That would also be the way I would do it. You could also use array_key_exists() to make sure that the correct form field exists in the $_FILES array if you want
    Okay, thanks Tango!!


    Debbie


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •