How can I get the URL of each uploaded file and send through email?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















OVERVIEW:
I have a form that includes 5 file upload fields. A php script processes the data and sends out 2 emails (one to the admin and a confirmation receipt to the user) AND appends the data to a .csv file on the server.



QUESTION:
How do I get the URL of the uploaded files in to a variable that I can use to populate the email and .csv file.



Everything is working great except I need a link to each of the the uploaded files included in the emails and in the .csv file. I cannot seem to figure that part out after a couple days of trying.



SIMPLIFIED HTML FORM:



<HTML><head><title>MultiFile Upload and Send Email</title></head><body>         
<form method='post' action='php/multiUploadTestProcess.php' multipart='' enctype='multipart/form-data'>
<label for ='exhName'>Exhibitor Name:</label>
<input type='text' name='exhName' multiple class='form-control'><br><br>
Select File 1: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
Select File 2: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
Select File 3: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
Select File 4: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
Select File 5: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
<input type='submit' value='upload'>
</form>




THE PHP: NOTE: I've removed all the validation/sanitizing, removed .csv appending, and the second email submission. I'm assuming once we can get the links in to one email the rest will be pretty much the same.



<?php
$exhName = $_POST['exhName'];
$exhNameNoSpace = str_replace(" ","-", $exhName);
$img = $_FILES['img'];
$to = 'DESTINATION-EMAIL@DOMAIN.com';
$subject = 'New File Uploads';
$email = 'ReplyYToEMAIL@DOMAIN.com';

if(!empty($img))
{
/* reArrayFiles changes the array pattern for PHP's $_FILES (see function at end) */
$img_desc = reArrayFiles($img);
print_r($img_desc);

/* RENAME EACH FILE to current date, exhibitor name w/o spaces, and random integer string (ensuring no file overwrites). Then MOVE FILE to uploads */
foreach($img_desc as $val)
{
$newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
/* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
$filePath = '**Full Path To Directory**'.$newname;
}
/* SEND EMAIL TO ADMIN */
$emailText = "=============================================nYou have a new Market Place Applicationn".
"Here are the details:n=============================================nn Exhibitor Name: $exhName nn Upload File 1: $filePath nn Upload File 2: $filePath nn ###";

// All the neccessary headers for the email.
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $email,
'Reply-To: ' . $email,
'Return-Path: ' . $email,
);
$emailText = wordwrap($emailText, 70);
// Send email
mail($to, $subject, $emailText, implode("n", $headers));
/* END SEND EMAIL TO ADMIN */
}
function reArrayFiles($file)
{
$file_ary = array();
$file_count = count($file['name']);
$file_key = array_keys($file);

for($i=0;$i<$file_count;$i++)
{
foreach($file_key as $val)
{
$file_ary[$i][$val] = $file[$val][$i];
}
}
return $file_ary;
}
?>


Help is greatly appreciated!










share|improve this question





























    0















    OVERVIEW:
    I have a form that includes 5 file upload fields. A php script processes the data and sends out 2 emails (one to the admin and a confirmation receipt to the user) AND appends the data to a .csv file on the server.



    QUESTION:
    How do I get the URL of the uploaded files in to a variable that I can use to populate the email and .csv file.



    Everything is working great except I need a link to each of the the uploaded files included in the emails and in the .csv file. I cannot seem to figure that part out after a couple days of trying.



    SIMPLIFIED HTML FORM:



    <HTML><head><title>MultiFile Upload and Send Email</title></head><body>         
    <form method='post' action='php/multiUploadTestProcess.php' multipart='' enctype='multipart/form-data'>
    <label for ='exhName'>Exhibitor Name:</label>
    <input type='text' name='exhName' multiple class='form-control'><br><br>
    Select File 1: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
    Select File 2: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
    Select File 3: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
    Select File 4: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
    Select File 5: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
    <input type='submit' value='upload'>
    </form>




    THE PHP: NOTE: I've removed all the validation/sanitizing, removed .csv appending, and the second email submission. I'm assuming once we can get the links in to one email the rest will be pretty much the same.



    <?php
    $exhName = $_POST['exhName'];
    $exhNameNoSpace = str_replace(" ","-", $exhName);
    $img = $_FILES['img'];
    $to = 'DESTINATION-EMAIL@DOMAIN.com';
    $subject = 'New File Uploads';
    $email = 'ReplyYToEMAIL@DOMAIN.com';

    if(!empty($img))
    {
    /* reArrayFiles changes the array pattern for PHP's $_FILES (see function at end) */
    $img_desc = reArrayFiles($img);
    print_r($img_desc);

    /* RENAME EACH FILE to current date, exhibitor name w/o spaces, and random integer string (ensuring no file overwrites). Then MOVE FILE to uploads */
    foreach($img_desc as $val)
    {
    $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
    move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
    /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
    $filePath = '**Full Path To Directory**'.$newname;
    }
    /* SEND EMAIL TO ADMIN */
    $emailText = "=============================================nYou have a new Market Place Applicationn".
    "Here are the details:n=============================================nn Exhibitor Name: $exhName nn Upload File 1: $filePath nn Upload File 2: $filePath nn ###";

    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
    'From: ' . $email,
    'Reply-To: ' . $email,
    'Return-Path: ' . $email,
    );
    $emailText = wordwrap($emailText, 70);
    // Send email
    mail($to, $subject, $emailText, implode("n", $headers));
    /* END SEND EMAIL TO ADMIN */
    }
    function reArrayFiles($file)
    {
    $file_ary = array();
    $file_count = count($file['name']);
    $file_key = array_keys($file);

    for($i=0;$i<$file_count;$i++)
    {
    foreach($file_key as $val)
    {
    $file_ary[$i][$val] = $file[$val][$i];
    }
    }
    return $file_ary;
    }
    ?>


    Help is greatly appreciated!










    share|improve this question

























      0












      0








      0








      OVERVIEW:
      I have a form that includes 5 file upload fields. A php script processes the data and sends out 2 emails (one to the admin and a confirmation receipt to the user) AND appends the data to a .csv file on the server.



      QUESTION:
      How do I get the URL of the uploaded files in to a variable that I can use to populate the email and .csv file.



      Everything is working great except I need a link to each of the the uploaded files included in the emails and in the .csv file. I cannot seem to figure that part out after a couple days of trying.



      SIMPLIFIED HTML FORM:



      <HTML><head><title>MultiFile Upload and Send Email</title></head><body>         
      <form method='post' action='php/multiUploadTestProcess.php' multipart='' enctype='multipart/form-data'>
      <label for ='exhName'>Exhibitor Name:</label>
      <input type='text' name='exhName' multiple class='form-control'><br><br>
      Select File 1: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      Select File 2: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      Select File 3: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      Select File 4: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      Select File 5: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      <input type='submit' value='upload'>
      </form>




      THE PHP: NOTE: I've removed all the validation/sanitizing, removed .csv appending, and the second email submission. I'm assuming once we can get the links in to one email the rest will be pretty much the same.



      <?php
      $exhName = $_POST['exhName'];
      $exhNameNoSpace = str_replace(" ","-", $exhName);
      $img = $_FILES['img'];
      $to = 'DESTINATION-EMAIL@DOMAIN.com';
      $subject = 'New File Uploads';
      $email = 'ReplyYToEMAIL@DOMAIN.com';

      if(!empty($img))
      {
      /* reArrayFiles changes the array pattern for PHP's $_FILES (see function at end) */
      $img_desc = reArrayFiles($img);
      print_r($img_desc);

      /* RENAME EACH FILE to current date, exhibitor name w/o spaces, and random integer string (ensuring no file overwrites). Then MOVE FILE to uploads */
      foreach($img_desc as $val)
      {
      $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
      move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
      /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
      $filePath = '**Full Path To Directory**'.$newname;
      }
      /* SEND EMAIL TO ADMIN */
      $emailText = "=============================================nYou have a new Market Place Applicationn".
      "Here are the details:n=============================================nn Exhibitor Name: $exhName nn Upload File 1: $filePath nn Upload File 2: $filePath nn ###";

      // All the neccessary headers for the email.
      $headers = array('Content-Type: text/plain; charset="UTF-8";',
      'From: ' . $email,
      'Reply-To: ' . $email,
      'Return-Path: ' . $email,
      );
      $emailText = wordwrap($emailText, 70);
      // Send email
      mail($to, $subject, $emailText, implode("n", $headers));
      /* END SEND EMAIL TO ADMIN */
      }
      function reArrayFiles($file)
      {
      $file_ary = array();
      $file_count = count($file['name']);
      $file_key = array_keys($file);

      for($i=0;$i<$file_count;$i++)
      {
      foreach($file_key as $val)
      {
      $file_ary[$i][$val] = $file[$val][$i];
      }
      }
      return $file_ary;
      }
      ?>


      Help is greatly appreciated!










      share|improve this question














      OVERVIEW:
      I have a form that includes 5 file upload fields. A php script processes the data and sends out 2 emails (one to the admin and a confirmation receipt to the user) AND appends the data to a .csv file on the server.



      QUESTION:
      How do I get the URL of the uploaded files in to a variable that I can use to populate the email and .csv file.



      Everything is working great except I need a link to each of the the uploaded files included in the emails and in the .csv file. I cannot seem to figure that part out after a couple days of trying.



      SIMPLIFIED HTML FORM:



      <HTML><head><title>MultiFile Upload and Send Email</title></head><body>         
      <form method='post' action='php/multiUploadTestProcess.php' multipart='' enctype='multipart/form-data'>
      <label for ='exhName'>Exhibitor Name:</label>
      <input type='text' name='exhName' multiple class='form-control'><br><br>
      Select File 1: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      Select File 2: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      Select File 3: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      Select File 4: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      Select File 5: <input type='file' name='img' multiple class='form-control' size='10'><br><br>
      <input type='submit' value='upload'>
      </form>




      THE PHP: NOTE: I've removed all the validation/sanitizing, removed .csv appending, and the second email submission. I'm assuming once we can get the links in to one email the rest will be pretty much the same.



      <?php
      $exhName = $_POST['exhName'];
      $exhNameNoSpace = str_replace(" ","-", $exhName);
      $img = $_FILES['img'];
      $to = 'DESTINATION-EMAIL@DOMAIN.com';
      $subject = 'New File Uploads';
      $email = 'ReplyYToEMAIL@DOMAIN.com';

      if(!empty($img))
      {
      /* reArrayFiles changes the array pattern for PHP's $_FILES (see function at end) */
      $img_desc = reArrayFiles($img);
      print_r($img_desc);

      /* RENAME EACH FILE to current date, exhibitor name w/o spaces, and random integer string (ensuring no file overwrites). Then MOVE FILE to uploads */
      foreach($img_desc as $val)
      {
      $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
      move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
      /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
      $filePath = '**Full Path To Directory**'.$newname;
      }
      /* SEND EMAIL TO ADMIN */
      $emailText = "=============================================nYou have a new Market Place Applicationn".
      "Here are the details:n=============================================nn Exhibitor Name: $exhName nn Upload File 1: $filePath nn Upload File 2: $filePath nn ###";

      // All the neccessary headers for the email.
      $headers = array('Content-Type: text/plain; charset="UTF-8";',
      'From: ' . $email,
      'Reply-To: ' . $email,
      'Return-Path: ' . $email,
      );
      $emailText = wordwrap($emailText, 70);
      // Send email
      mail($to, $subject, $emailText, implode("n", $headers));
      /* END SEND EMAIL TO ADMIN */
      }
      function reArrayFiles($file)
      {
      $file_ary = array();
      $file_count = count($file['name']);
      $file_key = array_keys($file);

      for($i=0;$i<$file_count;$i++)
      {
      foreach($file_key as $val)
      {
      $file_ary[$i][$val] = $file[$val][$i];
      }
      }
      return $file_ary;
      }
      ?>


      Help is greatly appreciated!







      php phpmailer multiple-file-upload






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 19:28









      reciprocityFailurereciprocityFailure

      11




      11
























          1 Answer
          1






          active

          oldest

          votes


















          1
















          1. If you want to attach the file to your emails, you may need the fullpath on the server rather than external URL. Have a look at realpath PHP function and try to wrap the path you saved the file into this function call, so making it something like:



            $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
            move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
            /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
            $filePath = realpath('../uploads/'.$newname);



          after this $filepath should contain the full path to the uploaded file on the server.




          1. If you wish to add a link with an external URL into your mails, you really need the full URL, but it depends on the domain name and the location of uploads folder relatively to the root (/) of your domain. It should be something like http://example.com/uploads/11-16-2018-name-6534.jpg and you can figure it out by testing the access to the generated filenames via your browser, assuming your uploads folder is accessible through the webserver. Once you figure out the URL you may either save the domain and path into your config file or you may check if $_SERVER['SERVER_NAME'] contains the domain name and append it with the path then.


          In order to use the URLs in the email text you need to modify your code like:



          $emailText = "=============================================nYou have a new Market Place ApplicationnHere are the details:n=============================================nn Exhibitor Name: $exhName nn ";
          $i = 1;
          foreach($img_desc as $val)
          {
          $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
          move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
          /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
          $filePath = '**Full Path To Directory**'.$newname;
          $emailText .= "Upload File " . $i . ": " . $filePath . "nn";
          $i++;
          }

          // All the neccessary headers for the email.
          $headers = array('Content-Type: text/plain; charset="UTF-8";',
          'From: ' . $email,
          'Reply-To: ' . $email,
          'Return-Path: ' . $email,
          );
          $emailText = wordwrap($emailText, 70);
          // Send email
          mail($to, $subject, $emailText, implode("n", $headers));





          share|improve this answer


























          • Thank you for the response! I looked in to realPath and tried your solution above. At first is did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' and it did return the path to the file. However

            – reciprocityFailure
            Nov 16 '18 at 20:37













          • so is that what you expected? It wasn't really clear in your question if you wanted the path on the server or the public URL of the file

            – Alexey
            Nov 16 '18 at 20:38











          • Rewriting my comment - sorry, new to this forum!

            – reciprocityFailure
            Nov 16 '18 at 20:48











          • Thank you for response! Tried your solution. At first it did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' which returned path to the file. However, the file name returned contained a different rndm string than actual file. Also returned the same file name for each file, not the individual names. Looking to have public URL of files included in emails & .csv. Public URL is not an issue, returning the file name for each of the files is problem I can't seem to fix.

            – reciprocityFailure
            Nov 16 '18 at 20:49











          • if you dump realpath('../uploads/').$newname inside the foreach loop you should see different filenames and all of them should be correct

            – Alexey
            Nov 16 '18 at 20:53












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344213%2fhow-can-i-get-the-url-of-each-uploaded-file-and-send-through-email%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1
















          1. If you want to attach the file to your emails, you may need the fullpath on the server rather than external URL. Have a look at realpath PHP function and try to wrap the path you saved the file into this function call, so making it something like:



            $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
            move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
            /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
            $filePath = realpath('../uploads/'.$newname);



          after this $filepath should contain the full path to the uploaded file on the server.




          1. If you wish to add a link with an external URL into your mails, you really need the full URL, but it depends on the domain name and the location of uploads folder relatively to the root (/) of your domain. It should be something like http://example.com/uploads/11-16-2018-name-6534.jpg and you can figure it out by testing the access to the generated filenames via your browser, assuming your uploads folder is accessible through the webserver. Once you figure out the URL you may either save the domain and path into your config file or you may check if $_SERVER['SERVER_NAME'] contains the domain name and append it with the path then.


          In order to use the URLs in the email text you need to modify your code like:



          $emailText = "=============================================nYou have a new Market Place ApplicationnHere are the details:n=============================================nn Exhibitor Name: $exhName nn ";
          $i = 1;
          foreach($img_desc as $val)
          {
          $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
          move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
          /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
          $filePath = '**Full Path To Directory**'.$newname;
          $emailText .= "Upload File " . $i . ": " . $filePath . "nn";
          $i++;
          }

          // All the neccessary headers for the email.
          $headers = array('Content-Type: text/plain; charset="UTF-8";',
          'From: ' . $email,
          'Reply-To: ' . $email,
          'Return-Path: ' . $email,
          );
          $emailText = wordwrap($emailText, 70);
          // Send email
          mail($to, $subject, $emailText, implode("n", $headers));





          share|improve this answer


























          • Thank you for the response! I looked in to realPath and tried your solution above. At first is did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' and it did return the path to the file. However

            – reciprocityFailure
            Nov 16 '18 at 20:37













          • so is that what you expected? It wasn't really clear in your question if you wanted the path on the server or the public URL of the file

            – Alexey
            Nov 16 '18 at 20:38











          • Rewriting my comment - sorry, new to this forum!

            – reciprocityFailure
            Nov 16 '18 at 20:48











          • Thank you for response! Tried your solution. At first it did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' which returned path to the file. However, the file name returned contained a different rndm string than actual file. Also returned the same file name for each file, not the individual names. Looking to have public URL of files included in emails & .csv. Public URL is not an issue, returning the file name for each of the files is problem I can't seem to fix.

            – reciprocityFailure
            Nov 16 '18 at 20:49











          • if you dump realpath('../uploads/').$newname inside the foreach loop you should see different filenames and all of them should be correct

            – Alexey
            Nov 16 '18 at 20:53
















          1
















          1. If you want to attach the file to your emails, you may need the fullpath on the server rather than external URL. Have a look at realpath PHP function and try to wrap the path you saved the file into this function call, so making it something like:



            $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
            move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
            /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
            $filePath = realpath('../uploads/'.$newname);



          after this $filepath should contain the full path to the uploaded file on the server.




          1. If you wish to add a link with an external URL into your mails, you really need the full URL, but it depends on the domain name and the location of uploads folder relatively to the root (/) of your domain. It should be something like http://example.com/uploads/11-16-2018-name-6534.jpg and you can figure it out by testing the access to the generated filenames via your browser, assuming your uploads folder is accessible through the webserver. Once you figure out the URL you may either save the domain and path into your config file or you may check if $_SERVER['SERVER_NAME'] contains the domain name and append it with the path then.


          In order to use the URLs in the email text you need to modify your code like:



          $emailText = "=============================================nYou have a new Market Place ApplicationnHere are the details:n=============================================nn Exhibitor Name: $exhName nn ";
          $i = 1;
          foreach($img_desc as $val)
          {
          $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
          move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
          /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
          $filePath = '**Full Path To Directory**'.$newname;
          $emailText .= "Upload File " . $i . ": " . $filePath . "nn";
          $i++;
          }

          // All the neccessary headers for the email.
          $headers = array('Content-Type: text/plain; charset="UTF-8";',
          'From: ' . $email,
          'Reply-To: ' . $email,
          'Return-Path: ' . $email,
          );
          $emailText = wordwrap($emailText, 70);
          // Send email
          mail($to, $subject, $emailText, implode("n", $headers));





          share|improve this answer


























          • Thank you for the response! I looked in to realPath and tried your solution above. At first is did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' and it did return the path to the file. However

            – reciprocityFailure
            Nov 16 '18 at 20:37













          • so is that what you expected? It wasn't really clear in your question if you wanted the path on the server or the public URL of the file

            – Alexey
            Nov 16 '18 at 20:38











          • Rewriting my comment - sorry, new to this forum!

            – reciprocityFailure
            Nov 16 '18 at 20:48











          • Thank you for response! Tried your solution. At first it did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' which returned path to the file. However, the file name returned contained a different rndm string than actual file. Also returned the same file name for each file, not the individual names. Looking to have public URL of files included in emails & .csv. Public URL is not an issue, returning the file name for each of the files is problem I can't seem to fix.

            – reciprocityFailure
            Nov 16 '18 at 20:49











          • if you dump realpath('../uploads/').$newname inside the foreach loop you should see different filenames and all of them should be correct

            – Alexey
            Nov 16 '18 at 20:53














          1












          1








          1









          1. If you want to attach the file to your emails, you may need the fullpath on the server rather than external URL. Have a look at realpath PHP function and try to wrap the path you saved the file into this function call, so making it something like:



            $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
            move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
            /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
            $filePath = realpath('../uploads/'.$newname);



          after this $filepath should contain the full path to the uploaded file on the server.




          1. If you wish to add a link with an external URL into your mails, you really need the full URL, but it depends on the domain name and the location of uploads folder relatively to the root (/) of your domain. It should be something like http://example.com/uploads/11-16-2018-name-6534.jpg and you can figure it out by testing the access to the generated filenames via your browser, assuming your uploads folder is accessible through the webserver. Once you figure out the URL you may either save the domain and path into your config file or you may check if $_SERVER['SERVER_NAME'] contains the domain name and append it with the path then.


          In order to use the URLs in the email text you need to modify your code like:



          $emailText = "=============================================nYou have a new Market Place ApplicationnHere are the details:n=============================================nn Exhibitor Name: $exhName nn ";
          $i = 1;
          foreach($img_desc as $val)
          {
          $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
          move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
          /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
          $filePath = '**Full Path To Directory**'.$newname;
          $emailText .= "Upload File " . $i . ": " . $filePath . "nn";
          $i++;
          }

          // All the neccessary headers for the email.
          $headers = array('Content-Type: text/plain; charset="UTF-8";',
          'From: ' . $email,
          'Reply-To: ' . $email,
          'Return-Path: ' . $email,
          );
          $emailText = wordwrap($emailText, 70);
          // Send email
          mail($to, $subject, $emailText, implode("n", $headers));





          share|improve this answer

















          1. If you want to attach the file to your emails, you may need the fullpath on the server rather than external URL. Have a look at realpath PHP function and try to wrap the path you saved the file into this function call, so making it something like:



            $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
            move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
            /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
            $filePath = realpath('../uploads/'.$newname);



          after this $filepath should contain the full path to the uploaded file on the server.




          1. If you wish to add a link with an external URL into your mails, you really need the full URL, but it depends on the domain name and the location of uploads folder relatively to the root (/) of your domain. It should be something like http://example.com/uploads/11-16-2018-name-6534.jpg and you can figure it out by testing the access to the generated filenames via your browser, assuming your uploads folder is accessible through the webserver. Once you figure out the URL you may either save the domain and path into your config file or you may check if $_SERVER['SERVER_NAME'] contains the domain name and append it with the path then.


          In order to use the URLs in the email text you need to modify your code like:



          $emailText = "=============================================nYou have a new Market Place ApplicationnHere are the details:n=============================================nn Exhibitor Name: $exhName nn ";
          $i = 1;
          foreach($img_desc as $val)
          {
          $newname = date('m-d-Y',time()).'-'.$exhNameNoSpace.'-'.mt_rand().'.jpg';
          move_uploaded_file($val['tmp_name'],'../uploads/'.$newname);
          /* THIS FOLLOWING LINE WAS MY ATTEMPT AT GETTING A LINK TO EACH UPLOADED FILE BUT IS NOT WORKING. AND THE VARIABLE $newname HERE IS ADDING A NEW RANDOM INT TO THE FILENAME - NOT THE ONE THE FILE WAS SAVED WITH. */
          $filePath = '**Full Path To Directory**'.$newname;
          $emailText .= "Upload File " . $i . ": " . $filePath . "nn";
          $i++;
          }

          // All the neccessary headers for the email.
          $headers = array('Content-Type: text/plain; charset="UTF-8";',
          'From: ' . $email,
          'Reply-To: ' . $email,
          'Return-Path: ' . $email,
          );
          $emailText = wordwrap($emailText, 70);
          // Send email
          mail($to, $subject, $emailText, implode("n", $headers));






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 22:08

























          answered Nov 16 '18 at 19:47









          AlexeyAlexey

          2,08752037




          2,08752037













          • Thank you for the response! I looked in to realPath and tried your solution above. At first is did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' and it did return the path to the file. However

            – reciprocityFailure
            Nov 16 '18 at 20:37













          • so is that what you expected? It wasn't really clear in your question if you wanted the path on the server or the public URL of the file

            – Alexey
            Nov 16 '18 at 20:38











          • Rewriting my comment - sorry, new to this forum!

            – reciprocityFailure
            Nov 16 '18 at 20:48











          • Thank you for response! Tried your solution. At first it did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' which returned path to the file. However, the file name returned contained a different rndm string than actual file. Also returned the same file name for each file, not the individual names. Looking to have public URL of files included in emails & .csv. Public URL is not an issue, returning the file name for each of the files is problem I can't seem to fix.

            – reciprocityFailure
            Nov 16 '18 at 20:49











          • if you dump realpath('../uploads/').$newname inside the foreach loop you should see different filenames and all of them should be correct

            – Alexey
            Nov 16 '18 at 20:53



















          • Thank you for the response! I looked in to realPath and tried your solution above. At first is did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' and it did return the path to the file. However

            – reciprocityFailure
            Nov 16 '18 at 20:37













          • so is that what you expected? It wasn't really clear in your question if you wanted the path on the server or the public URL of the file

            – Alexey
            Nov 16 '18 at 20:38











          • Rewriting my comment - sorry, new to this forum!

            – reciprocityFailure
            Nov 16 '18 at 20:48











          • Thank you for response! Tried your solution. At first it did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' which returned path to the file. However, the file name returned contained a different rndm string than actual file. Also returned the same file name for each file, not the individual names. Looking to have public URL of files included in emails & .csv. Public URL is not an issue, returning the file name for each of the files is problem I can't seem to fix.

            – reciprocityFailure
            Nov 16 '18 at 20:49











          • if you dump realpath('../uploads/').$newname inside the foreach loop you should see different filenames and all of them should be correct

            – Alexey
            Nov 16 '18 at 20:53

















          Thank you for the response! I looked in to realPath and tried your solution above. At first is did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' and it did return the path to the file. However

          – reciprocityFailure
          Nov 16 '18 at 20:37







          Thank you for the response! I looked in to realPath and tried your solution above. At first is did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' and it did return the path to the file. However

          – reciprocityFailure
          Nov 16 '18 at 20:37















          so is that what you expected? It wasn't really clear in your question if you wanted the path on the server or the public URL of the file

          – Alexey
          Nov 16 '18 at 20:38





          so is that what you expected? It wasn't really clear in your question if you wanted the path on the server or the public URL of the file

          – Alexey
          Nov 16 '18 at 20:38













          Rewriting my comment - sorry, new to this forum!

          – reciprocityFailure
          Nov 16 '18 at 20:48





          Rewriting my comment - sorry, new to this forum!

          – reciprocityFailure
          Nov 16 '18 at 20:48













          Thank you for response! Tried your solution. At first it did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' which returned path to the file. However, the file name returned contained a different rndm string than actual file. Also returned the same file name for each file, not the individual names. Looking to have public URL of files included in emails & .csv. Public URL is not an issue, returning the file name for each of the files is problem I can't seem to fix.

          – reciprocityFailure
          Nov 16 '18 at 20:49





          Thank you for response! Tried your solution. At first it did not return anything, then I changed this line: '$filePath = realpath('../uploads/'.$newname);' to '$filePath = realpath('../uploads/').$newname;' which returned path to the file. However, the file name returned contained a different rndm string than actual file. Also returned the same file name for each file, not the individual names. Looking to have public URL of files included in emails & .csv. Public URL is not an issue, returning the file name for each of the files is problem I can't seem to fix.

          – reciprocityFailure
          Nov 16 '18 at 20:49













          if you dump realpath('../uploads/').$newname inside the foreach loop you should see different filenames and all of them should be correct

          – Alexey
          Nov 16 '18 at 20:53





          if you dump realpath('../uploads/').$newname inside the foreach loop you should see different filenames and all of them should be correct

          – Alexey
          Nov 16 '18 at 20:53




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344213%2fhow-can-i-get-the-url-of-each-uploaded-file-and-send-through-email%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine