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

前端 未结 1 700
春和景丽
春和景丽 2021-01-27 08:23

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

相关标签:
1条回答
  • 2021-01-27 08:58
    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 Application\nHere are the details:\n=============================================\n\n Exhibitor Name: $exhName \n\n ";
    $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 . "\n\n";
        $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));
    
    0 讨论(0)
提交回复
热议问题