digitalgod Posted November 14, 2006 Share Posted November 14, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/27260-isset-problems-solved/ Share on other sites More sharing options...
printf Posted November 14, 2006 Share Posted November 14, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/27260-isset-problems-solved/#findComment-124730 Share on other sites More sharing options...
Nhoj Posted November 14, 2006 Share Posted November 14, 2006 I think what you should be using is[code]if (empty($_FILES['file1'])) { echo 'sorry, you cannot leave the file field empty';} else { do whatever}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27260-isset-problems-solved/#findComment-124761 Share on other sites More sharing options...
digitalgod Posted November 15, 2006 Author Share Posted November 15, 2006 alright thanks guys.For some reason I always thought that if a variable was empty it would mean that it's NULL Quote Link to comment https://forums.phpfreaks.com/topic/27260-isset-problems-solved/#findComment-124857 Share on other sites More sharing options...
wildteen88 Posted November 15, 2006 Share Posted November 15, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/27260-isset-problems-solved/#findComment-125069 Share on other sites More sharing options...
digitalgod Posted November 15, 2006 Author Share Posted November 15, 2006 thanks for the clarification wildteen Quote Link to comment https://forums.phpfreaks.com/topic/27260-isset-problems-solved/#findComment-125231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.