1

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.

5
  • 3
    include the full form Commented Dec 19, 2016 at 21:36
  • What do you mean "the paths get saved"? I see no code that "saves the path" anywhere. And this could be simplified if ( empty( $errors ) == TRUE ) to simply if ( ! $errors ) {.... Commented Dec 19, 2016 at 21:37
  • Did you add enctype? Commented Dec 19, 2016 at 21:39
  • 1
    @PraveenKumar yup no enctype now go answer for cheap points Commented Dec 19, 2016 at 21:41
  • @cale_b thank you. I did not know about the file path @Preveen I did not know about codeenctype Commented Dec 19, 2016 at 21:42

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

14 Comments

ACtually, this did not work. Could it be because I added this to my whole PhP actions file?
@SeanRawles How did you add? Everything looks correct for me.
Also, honestly, why did this get a down vote? Is it not okay to ask questions?
@SeanRawles It was put when the necessary info wasn't there. I didn't downvote BTW.
yeah. No idea how to do that. Again - trying to learn :D I am sure I will get it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.