scoopzilla
scoopzilla

Reputation: 883

cannot get file to upload in PhP

I have added this to my form_action.php:

if(isset($_FILES['attachment1'])){
  $errors= array();
  $file_name = $_FILES['attachment1']['name'];
  $file_size = $_FILES['attachment1']['size'];
  $file_tmp = $_FILES['attachment1']['tmp_name'];
  $file_type = $_FILES['attachment1']['type'];
  $file_ext=strtolower(end(explode('.',$_FILES['attachment1']['name'])));

  $expensions= array("jpeg","jpg","png","doc","pdf");

  if(in_array($file_ext,$expensions)=== false){
     $errors[]="extension not allowed, please choose a JPEG, PNG, PDF or DOC file.";
  }

  if(empty($errors)==true) {
     move_uploaded_file($file_tmp,"uploads/".$file_name);
     echo "Success";
  }else{
     print_r($errors);
  }
}

with the form field being

<form method="post" action="form_action1.php">
<div class="form-group">
     <label for="attachment1">Add file(s)</label>
     <input type="file" class="form-control-file" id="attachment1" aria-describedby="fileHelp" name="attachment1">
     <input type="file" class="form-control-file" id="attachment2" aria-describedby="fileHelp" name="attachment2">
     <input type="file" class="form-control-file" id="attachment3" aria-describedby="fileHelp" name="attachment3">       
 </div>
</form>

The path gets saved but the file does not upload. Am I missing a step that I am not aware of? Also, can I set an ARRAY in the $_FILES field to allow for three uploads?

This is the last step in a personal project that I am trying to complete. Thank you.

Upvotes: 1

Views: 71

Answers (1)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

You need to add enctype for the form to post files.

<form method="post" action="form_action1.php" enctype="multipart/form-data">

From MDN:

enctype

When the value of the method attribute is post, enctype is the MIME type of content that is used to submit the form to the server. Possible values are: application/x-www-form-urlencoded: The default value if the attribute is not specified.

  • multipart/form-data: The value used for an element with the type attribute set to "file".
  • text/plain (HTML5): This value can be overridden by a formenctype attribute on a or element.

Upvotes: 2

Related Questions