Jump to content

isset problems *solved*


digitalgod

Recommended Posts

hey guys,

why does
[code=php:0]
if (isset($_FILES['file1'])) {
[/code]
always return true, so the if statement is always processed, even though my file field (file1) is empty??
It's just a basic form that pulls info from the db and let's a user modify that info. I just can't understand why is it that when I [code=php:0]echo isset($_FILES{'file1'])[/code] it returns 1 even though the field is empty....

any ideas?
Link to comment
Share on other sites

if the file input has no value, it does not mean the file input was not part of the form. isset() tells you only if the form input was part of the form or if GET was a member of the query string name value pairs! So you should check with...

[code]if ( is_upload_file ( $_FILES['file1'] ) )
{
// do stuff
}[/code]

Which tells you if you have a uploaded file. For POST and GET you would do it differently, depending on the style you like to use...

// one example

[code]if ( isset ( $_POST['foo'] ) && trim ( $_POST['foo'] ) != '' )
{
// do stuff
}[/code]


Link to comment
Share on other sites

Yes that is correct.

However the isset function does not check the value of a variable. Instead checks for the existence of a variable.

Also the reason why your code was returning true is because the $_FILE variable is created automatically with or without the field the variable is associated with is empty or not.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.