How can I get the Input file path?


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:


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


document.getElementById('addAnexos').onclick = function () {
  $('#pegarAnexo').change(function (event) {
      tmppath = URL.createObjectURL([0]);


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

   $mail = new PHPMailer;
   $mail->CharSet = 'UTF-8';
   $mail->Host = $aHost;
   $mail->Port = $aPort;
   $mail->SMTPSecure = 'tls';
   $mail->SMTPAuth = true;
   $mail->Username = $aUser;
   $mail->Password = $aPass;
   $mail->Subject = $aAssunto;
   $mail->Body = $aCorpo;

   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


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

Plugin usage code:

    <script src=""></script><scriptsrc=""></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!"); 

I placed GitHub for future reference .

From this you can go customizing according to your need.


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:


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:


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