How do I get in the Input file?

Asked

Viewed 8,622 times

4

I need to send several attachments via Phpmailer and am using jQuery to get the value of input file, now I need to pick up the file path to be able to send by email, but when I take the path it comes with C:\fakepath\image.jpg or only image.jpg, I’ve been doing research and this is related to browser security, so how do you send attachments? the schematic I’m doing and the following:

HTML

       <div class="divAnexos">
           <label for="anexos" class="label">Anexos:</label> &nbsp;
           <input type="file" id="pegarAnexo" multiple>
           <textarea id="anexos"></textarea>
           <button id="addAnexos" class="ui-state-default"> Anexar </button>
        </div>

JS

document.getElementById('addAnexos').onclick = function () {
      document.getElementById('pegarAnexo').click();
   };
  $('#pegarAnexo').change(function (event) {
      tmppath = URL.createObjectURL(event.target.files[0]);
      console.log(tmppath);
      $('#anexos').html($(this).val());
   });

PHP

function enviarEmail($aUser, $aPass, $aPort, $aDestinatario, $aHost, $aAssunto, $aCorpo, $aArquivos = '', $aCopia = '') {

   $mail = new PHPMailer;
   $mail->isSMTP();
   $mail->CharSet = 'UTF-8';
   $mail->Host = $aHost;
   $mail->Port = $aPort;
   $mail->SMTPSecure = 'tls';
   $mail->SMTPAuth = true;
   $mail->Username = $aUser;
   $mail->Password = $aPass;
   $mail->setFrom($aUser);
   $mail->addAddress($aDestinatario);
   $mail->addAddress($aCopia);
   $mail->Subject = $aAssunto;
   $mail->Body = $aCorpo;
   $mail->addAttachment($aArquivos);

   if (!$mail->send()) {
      return false;
   } else {
      return true;
   }
}

The algorithm does the following when you click the button addAnexos he will open the input file to pick up the file and put the path information in the textarea, the tmppath comes from an idea I picked up here, which takes the temporary file path, but this path only works on Google Chrome and Mozilla.

  • Why do you need to know path to send the file? The intention is to send the file and this HTML does for you. If you want to use JS to automate, ok, but the path is not necessary.

  • biggown, how to send attachments without the need only with html ? I have this doubt, because I pass in the function sendEmail the parameter File, which contains a string with the path.

  • What a function enviarEmail? I don’t see any.

  • You have to take a look at the file field, after understanding how the file upload works, you will get the physical path of the file to send it as an attachment

1 answer

4


That is not possible.

As you have read about it is related to security and privacy. There are no normal means to go through this. It would only be possible in a severe security browser failure and certainly does not solve your goal.

If you want to send a user file, it has to say what it wants to send without your interference as a programmer. Any additional information other than the file itself is not relevant to your code. I can’t imagine any application for this in a browser.

If you must know the path for some other reason, do not use a browser as a platform. There is no way.

Solution for sending by AJAX

The most common is to use ready-made solutions. As you are using jQuery I think it would be useful to see the plugin jQuery Form. Example of documentation:

<form id="myForm" action="comment.php" method="post"> 
    Name: <input type="text" name="name" /> 
    Comment: <textarea name="comment"></textarea> 
    <input type="submit" value="Submit Comment" /> 
</form>

Code p/ use of plugin:

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
 
    <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'myForm' and provide a simple callback function 
            $('#myForm').ajaxForm(function() { 
                alert("Thank you for your comment!"); 
            }); 
        }); 
    </script> 
</head>

I put in the Github for future reference.

From this you can go customizing according to your need.

PHP

The PHP code just has to receive data from the browser, it doesn’t need to know anything where the file was in the client. This is done with $FILES. Something like this:

$_FILES['file']['tmp_name']

By the code posted you’ve probably already done this and are storing the list of files somewhere and passes it by the parameter $aArquivos.

What you’re probably trying to do is this:

$mail->AddAttachment($_FILES['seuArquivo']['tmp_name'],
                     $_FILES['seuArquivo']['name']);

This allows you to keep a "presentable" name for the file when it is attached to the e-mail. Otherwise the name used will be the same placed in the temporary files directory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.