Jump to content

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

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
norbie

norbie

    Newbie

  • Members
  • PipPip
  • 12 posts
Hi guys,

I made/used an image uploader script today.

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

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
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
$_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.
print_r($_FILES['new_image']);
To access an element in the array, you need to ask for its exact index. For example
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: http://forum.codecal...load-class.html

#3
norbie

norbie

    Newbie

  • Members
  • PipPip
  • 12 posts
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
norbie

norbie

    Newbie

  • Members
  • PipPip
  • 12 posts
All sorted now, I just used:

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

Thanks

#5
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
cant you use

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

#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
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.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

"Xav" said:

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
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
100% true? Wow, this must be a first! :D

Say you have a variable:
$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:
$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:
$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.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums