Closed Thread
Results 1 to 8 of 8

Thread: Help with image upload script - need to make an if null statement

  1. #1
    norbie is offline Newbie
    Join Date
    Dec 2006
    Posts
    12
    Rep Power
    0

    Help with image upload script - need to make an if null statement

    Hi guys,

    I made/used an image uploader script today.

    This is how the code works now after POSTing the details with a form.

    Code:
    if (isset($_FILES['new_image'])){

    // do image stuff

    } else {

    // don't do image stuff


    Basically the image script does work but if the user doesn't enter anything into the image box, the rest of the form functions will work (entering info into a database) but it has messy errors on the page.

    How can I create some kind of if statement that says "if they havent entered anything into the image field then dont do stuff" ?

    I tried echoing $_FILES['new_image'] but it comes back as "array". I have not used $_FILES before so do not understand this.

    Thanks.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Help with image upload script - need to make an if null statement

    $_FILES is a global multi-dimensional array defined by php. If you would like to see the contents of the array you can use the print_r() function.
    Code:
    print_r($_FILES['new_image']); 
    To access an element in the array, you need to ask for its exact index. For example
    Code:
    echo $_FILES['new_image']['name']; 
    will echo the name of the file. See here for more information: PHP: Handling file uploads - Manual

    I've written a upload class that you can modify if you would like: PHP: Upload Class

  4. #3
    norbie is offline Newbie
    Join Date
    Dec 2006
    Posts
    12
    Rep Power
    0

    Re: Help with image upload script - need to make an if null statement

    Ah I see. Thanks John.

    The upload script all works now apart from this null issue so I'd rather not change it with a different system now but will look at yours.

    Is there a way I can add an "if null" check?

  5. #4
    norbie is offline Newbie
    Join Date
    Dec 2006
    Posts
    12
    Rep Power
    0

    Re: Help with image upload script - need to make an if null statement

    All sorted now, I just used:

    Code:
    if ($_FILES['new_image']['name'] == "") { 
    Thanks

  6. #5
    phpforfun's Avatar
    phpforfun is offline Speaks fluent binary
    Join Date
    Feb 2008
    Posts
    1,232
    Blog Entries
    17
    Rep Power
    24

    Re: Help with image upload script - need to make an if null statement

    cant you use

    if (empty($_FILES['new_image']['name'])){

  7. #6
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Help with image upload script - need to make an if null statement

    There are usually different ways to accomplish a task. In some circumstances, "" is not the same as null, so you need to be careful which one you use.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  8. #7
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Help with image upload script - need to make an if null statement

    Quote Originally Posted by Xav
    In some circumstances, "" is not the same as null, so you need to be careful which one you use.
    That is 100% true. From my experience with working $_GET, $_POST, or $_FILE they all seem to turn up different results. Often times !isset($var), $empty($var), $var == null, $var == "", and $var == " " will often turn up different results.

  9. #8
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Help with image upload script - need to make an if null statement

    100% true? Wow, this must be a first!

    Say you have a variable:
    Code:
    $var 
    If you don't assign any value to it, the variable has not been instantiated yet. Therefore, it contains no value at all - in other words, it's null.

    However, if you use:
    Code:
    $var ""
    A value is being passed into the variable. Even though it's got a length of 0, it's still a string value - just a blank one. Therefore, the variable is now not null, as it contains a value (albeit a blank one).

    Finally, if we pass a space to the variable:
    Code:
    $var " "
    Here, the value passed has a length of 1 - in programming, a space is considered as just another ASCII character, just like a letter or number. Therefore, this variable is also not null, as it holds a normal value - the only difference is, the value isn't visible to humans, as it's used to fill a gap.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Advanced Upload an image and downsize it (in KB`s) using php and GD
    By Csabi in forum PHP Tutorials
    Replies: 4
    Last Post: 01-22-2011, 10:07 PM
  2. Image upload and resize
    By Bioshox in forum PHP Development
    Replies: 1
    Last Post: 07-09-2010, 02:10 AM
  3. Upload Script?
    By BASHERS33 in forum PHP Development
    Replies: 8
    Last Post: 08-30-2009, 08:36 AM
  4. upload image and resieze..
    By techker in forum PHP Development
    Replies: 3
    Last Post: 10-01-2008, 11:07 PM
  5. Upload Script Help..
    By coated_pill in forum PHP Development
    Replies: 1
    Last Post: 07-14-2008, 08:46 AM

Tags for this Thread

Bookmarks

Posting Permissions

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