0

there seem to be a lot of people asking this question and not getting an answer. is there an issue with checking if a file upload part of a form has been filled in or not? is this because its a global variable and is allways set?

the problem im having is checking if a file input ytpe has been filled in or not.

the code below shows various examples of checking to see if the file field is filled in by the user or not.

they all fail to spot when its been filled in:

<?php
if (isset($_POST['add'])) 
{

//if (!isset($_POST['pic'])) { echo"pic not filled in";}else{echo"pic filled in";}

if (!isset($_POST['pic'])) { echo"pic not filled in";}
if (isset($_POST['pic']))  { echo"pic filled in";}//end of check to see if picture     has been filled in

//if (!isset($_POST['userfile[]'])) { echo"pic not filled in";}
//if (isset($_POST['userfile[]']))  { echo"pic filled in";}//end of check to see if   picture has been filled in

//$pictureName = $_REQUEST['pic'];
//if ($pictureName == ''){echo"pic is blank";}
//else{echo"pic is NOT blank";}

//if ($_POST['pic'] == ""){echo"pic is blank";}
//else {echo"pic is NOT blank";}
}

else 
{

 /////////render form
?>
<form enctype="multipart/form-data" action="" method="post" id="save"><fieldset>
<input type="text" name="fileName" id="fileName" value=""/>
<input type="file" name="userfile[]" id="pic" />
<input name="add" id="save_button" type="submit" value="Update"/>
</fieldset></form>
<?php

}
?>
as_bold_as_love
  • 216
  • 5
  • 11
  • I've had this same problem before. And it was because I was checking against ID attributes instead of NAME attributes. See Dagon's answer. – Daniel Carvalho Mar 06 '13 at 09:07

2 Answers2

1

because your using 'pic' not 'userfile'. is the html name that makes the post key value, not the id.

it should be userfile not userfile[] unless you have a multi input form

P.S I see no large number of unanswered questions of this type.

  • ive seen a few like this....i tried a few examples but coulnt get it to work. for example: `code`if (isset($_POST['userfile'])) { echo"pic filled in";}`code` – as_bold_as_love Feb 20 '13 at 23:14
  • there's no reason why the above should not work, please show a full example –  Feb 20 '13 at 23:16
1

i have found the answer ive been looking for finally. a massive thanks to You whos answer to this question helped me a lot:

the problem i was having was that if you check to see if a field in the form isset it will always be set once the submit button has been pressed.

if, like me, you want to check if something has actually been entered in to the field then i would say you need to do an if(!empty) check.

if the enctype="multipart/form-data" is added to the form this will stop working for file type fields.

please see my example below and thanks again to You:

<h1>test</h1>
    <p>check to see if a field in a form as been filled in:</p>
    <?php
    if (isset($_POST['info'])) {
      // do some stuff
      echo"info isset. it will always be set when you click the submit button</br>"; 

      if(!empty($_POST['info'])){ echo"info has been filled in</br>";}else{ echo"info is still empty</br>";}

      if(!empty($_POST['filename'])){ echo"filename has been filled in</br>";}else{ echo"filename is still empty</br>";}

    }
    else
    {
    ?>
    <form method="post">
    <input type="text" name="info" /></br>
    <input type="file" name="filename" /></br>
    <input type="submit" />
    </form>
    <?php }?>
Community
  • 1
  • 1
as_bold_as_love
  • 216
  • 5
  • 11