Hi guys,
I made/used an image uploader script today.
This is how the code works now after POSTing the details with a form.
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.Code:if (isset($_FILES['new_image'])){
// do image stuff
} else {
// don't do image stuff
}
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.
$_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.To access an element in the array, you need to ask for its exact index. For exampleCode:print_r($_FILES['new_image']);
will echo the name of the file. See here for more information: PHP: Handling file uploads - ManualCode:echo $_FILES['new_image']['name'];
I've written a upload class that you can modify if you would like: PHP: Upload Class
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?
All sorted now, I just used:
ThanksCode:if ($_FILES['new_image']['name'] == "") {
cant you use
if (empty($_FILES['new_image']['name'])){
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.Originally Posted by Xav
100% true? Wow, this must be a first!
Say you have a variable:
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.Code:$var
However, if you use:
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).Code:$var = "";
Finally, if we pass a space to the variable:
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.Code:$var = " ";
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks