+ Reply to Thread
Results 1 to 8 of 8

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

  1. #1
    Newbie norbie is an unknown quantity at this point
    Join Date
    Dec 2006
    Posts
    12

    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. #2
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,884
    Blog Entries
    25

    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

  3. #3
    Newbie norbie is an unknown quantity at this point
    Join Date
    Dec 2006
    Posts
    12

    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?

  4. #4
    Newbie norbie is an unknown quantity at this point
    Join Date
    Dec 2006
    Posts
    12

    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

  5. #5
    Speaks fluent binary phpforfun has a spectacular aura about phpforfun has a spectacular aura about phpforfun's Avatar
    Join Date
    Feb 2008
    Posts
    1,204
    Blog Entries
    17

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

    cant you use

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

  6. #6
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

  7. #7
    Co-Administrator John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John is a glorious beacon of light John's Avatar
    Join Date
    Jul 2006
    Age
    21
    Posts
    5,884
    Blog Entries
    25

    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.

  8. #8
    Xav
    Xav is offline
    Code Slinger Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav has much to be proud of Xav's Avatar
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,210
    Blog Entries
    13

    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Replies: 18
    Last Post: 07-08-2009, 03:55 PM
  2. How to make image straight - Photoshop
    By ahsan16 in forum Photoshop Tutorials
    Replies: 56
    Last Post: 07-30-2008, 03:11 PM
  3. An important exam!
    By Zael in forum C and C++
    Replies: 12
    Last Post: 01-22-2008, 09:08 PM
  4. Mysql/Php help need
    By mittalmak in forum PHP Forum
    Replies: 17
    Last Post: 01-12-2008, 12:36 PM
  5. Make a script vulnerable to SQL injection?
    By shibbythestoner in forum PHP Forum
    Replies: 7
    Last Post: 12-15-2007, 08:56 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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