0

I just went through this answer, as how to send a file in PHP using PHPMailer. I have the following HTML code:

<form action="./php/send-file.php" method="post" enctype="multipart/form-data">
              <input type="file" name="files" id="filer_input" multiple="multiple">
              <input type="submit" value="Submit">
</form>

PHP code as below:

require_once('class.phpmailer.php');

$email = new PHPMailer();
$email->From      = 'gautam@webmunky.com';
$email->FromName  = 'Your Name';
$email->Subject   = 'Message Subject';
$email->AddAddress( 'gautam@webmunky.com' );

<!-- $file_to_attach = $_FILES['files']; -->
$email->AddAttachment( $_FILES['files']['tmp_name'],
                         $_FILES['files']['name'] );
return $email->Send();

I believe I am making a mistake in the below two lines of code:

<!-- $file_to_attach = $_FILES['files']; -->
$email->AddAttachment( $_FILES['files']['tmp_name'],
                         $_FILES['files']['name'] );

but I am not sure. How can I send my file using the above PHP code?

halfer
  • 19,824
  • 17
  • 99
  • 186
Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174
  • Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details. Please [edit] these details in or we may not be able to help. – Blue Jul 30 '16 at 05:34
  • What is this `` that's not how we comment code in php blocks, just saying. Pretty sure that is a syntax error, change it to `//$file_to_attach = $_FILES['files'];` – ArtisticPhoenix Jul 30 '16 at 05:35
  • Store your image file from temp to your project root directory and then give path to attachment. – Nikhil Vaghela Jul 30 '16 at 05:35
  • Check this : http://stackoverflow.com/a/11764230/5830872 – Drone Jul 30 '16 at 05:35
  • How about looking at [the examples provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/) that do exactly what you ask, without the security holes? – Synchro Jul 30 '16 at 07:11

1 Answers1

0

Pretty sure this is a syntax error

<?php
      <!-- $file_to_attach = $_FILES['files']; -->

This is an HTML comment, when you want a PHP comment.

<?php
  //$file_to_attach = $_FILES['files'];

Or even

<?php
  #$file_to_attach = $_FILES['files'];
ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38