Jump to content

php form - check if file has been chosen or not


paulonline2501

Recommended Posts

hi all, i have an 'edit item page'.its for people to edit items in the database.when the chose an item to edit they get taken to a web page with a form on it.the form is filled with the current values in the database - apart from the picture field.i have left that balnk on perpose as im presuming they are happy with the pic.if they dont chose a new file and press save the details in the form [apart from the picture] update with the new values.if however they want to changer the pic and browse for a file i want this new pic to upload. therefore what im trying to do is do a check to see if the pic field has been filled in.if is has then run the upload file function.if the pic field has not been filled in then dont run the upload logic and only update the otther fields. im getting stuck where im doing the check. ive tried a bunch of things but they dont seem to work.my closest was as follows:

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

...it always returns: 'not filled in' CUT DOWN VERSION OF FULL SCRIPT

<?php//set current item ID$currentItemID = htmlspecialchars($_GET["id"]);// Setup defaults.$error  = 0; //input errors$up_error = 0; //title and description error counter - used to only show error message once.$clean = array();$clean_name = "";$clean_description = "";$clean_price = "";$clean_pic = "";$clean_status = "";$clean_quantity = "";//if all input is valid then...if (isset($_POST['add'])){ 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  }else{  /////////render form?><form enctype="multipart/form-data" action="" method="post" id="save"><fieldset><table id="site-form"><tr><td class="one_of_three"><label>Item Name:  </label></td><td class="two_of_three"><input type="text" name="fileName" id="fileName" value="<?php echo"$db_name";?>"/></td><td><label class="errors" id="fileNameError"> </label></td></tr> <tr><td class="one_of_three"><label>Picture:  </label></td><td class="two_of_three"><input type="file" name="userfile[]" id="pic"/></td><td><label class="errors" id="picError"> </label></td></tr> <tr>  <td class="one_of_three"> </td>  <td class="two_of_three"><input name="add" id="save_button" type="submit" value="Update"/>  <a href="../">Cancel</a>.</td>  <td> </td></tr></table></fieldset></form><?php  }?>

Edited by as_bold_as_love
Link to comment
Share on other sites

ive run all these echos and they result in blank values, wether i chose a file or not.

echo"</br>userfile = ";echo(($_POST['userfile']));echo"</br>pic = ";echo(($_POST['pic']));echo"</br>userfile = ";echo(($_POST[userfile]));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

Link to comment
Share on other sites

There's no $_POST['pic'] in that form. $_POST will contain "filename" and "add", the uploaded file will be in an array called $_FILES['userfile']. Since you made "userfile" an array, the first file they upload will be in $_FILES['userfile'][0].

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...