How can I get the Input file path?

3

I need to send multiple attachments via PHPmailer and I'm using jQuery to get the value of input file , now I need to get the file path to be able to send it by email, however when I get the path it comes with C:\fakepath\image.jpg or only image.jpg , I've been searching 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 addAnexos button it will open input file to get the file and put the path information in textarea , tmppath comes from an idea that I took here , which takes the temporary path of the file, but this path only works in Google Chrome and Mozilla.

    
asked by anonymous 04.12.2014 / 18:04

1 answer

3

This is not possible.

As you've read about it, it's about security and privacy. There are no normal ways to go through this. It would only be possible in a serious browser security flaw and it certainly does not solve its purpose.

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

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

AJAX Submission Solution

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 . Documentation example:

<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>

Plugin usage code:

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script><scriptsrc="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 placed GitHub for future reference .

From this you can go customizing according to your need.

PHP

PHP code only has to receive data from the browser, it does not need to know anything where the file was on the client. This is done with $ FILES. Something like this:

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

By the code posted you have probably done this and are storing the list of files somewhere and pass 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 email message. Otherwise the name used will be the same one placed in the temporary files directory.

    
04.12.2014 / 18:23