Lost Password?

Go Back   CodeCall Programming Forum > Web Development Forum > PHP Forum

PHP Forum Use this forum to discuss all aspects of PHP Development. PHP is a server-side, cross-platform, HTML embedded scripting language that lets you create dynamic web pages.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-23-2008, 04:29 PM
norbie norbie is offline
Newbie
 
Join Date: Dec 2006
Posts: 5
Rep Power: 0
norbie is on a distinguished road
Default 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.

PHP 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-23-2008, 05:03 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,364
Last Blog:
PHPUnit
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default 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.
PHP Code:
print_r($_FILES['new_image']); 
To access an element in the array, you need to ask for its exact index. For example
PHP 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
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-23-2008, 05:58 PM
norbie norbie is offline
Newbie
 
Join Date: Dec 2006
Posts: 5
Rep Power: 0
norbie is on a distinguished road
Default 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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-24-2008, 04:13 AM
norbie norbie is offline
Newbie
 
Join Date: Dec 2006
Posts: 5
Rep Power: 0
norbie is on a distinguished road
Default Re: Help with image upload script - need to make an if null statement

All sorted now, I just used:

PHP Code:
if ($_FILES['new_image']['name'] == "") { 
Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-01-2008, 03:45 PM
phpforfun's Avatar   
phpforfun phpforfun is offline
Programming God
 
Join Date: Feb 2008
Posts: 541
Last Blog:
ubuntu... do not upgra...
Rep Power: 5
phpforfun will become famous soon enough
Default Re: Help with image upload script - need to make an if null statement

cant you use

if (empty($_FILES['new_image']['name'])){
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-03-2008, 10:44 AM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 3,069
Last Blog:
Reading Jet Databases ...
Rep Power: 27
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default 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.
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-03-2008, 01:03 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,364
Last Blog:
PHPUnit
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default 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.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-04-2008, 09:44 AM
Xav's Avatar   
Xav Xav is offline
Guru
 
Join Date: Mar 2008
Location: London, England
Posts: 3,069
Last Blog:
Reading Jet Databases ...
Rep Power: 27
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default 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:
PHP 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:
PHP 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:
PHP 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.
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
VB 6.0: Tutorial, How to make a GIF in your application TcM VB Tutorials 17 07-01-2008 03:26 PM
How to make image straight - Photoshop ahsan16 Photoshop Tutorials 7 06-20-2008 10:38 AM
An important exam! Zael C and C++ 12 01-22-2008 09:08 PM
Mysql/Php help need mittalmak PHP Forum 17 01-12-2008 12:36 PM
Make a script vulnerable to SQL injection? shibbythestoner PHP Forum 7 12-15-2007 08:56 PM


All times are GMT -5. The time now is 01:02 AM.

Contest Stats

dargueta ........ 93.00000
John ........ 87.50000
Xav ........ 70.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads