如何获取每个上传文件的URL并通过电子邮件发送?

时间:2018-11-16 19:28:26

标签: php phpmailer multiple-file-upload

概述: 我有一个包含5个文件上传字段的表单。一个php脚本处理数据,并发出2封电子邮件(一封给管理员,一封确认收据给用户),并将数据附加到服务器上的.csv文件中。

问题: 如何将上传文件的URL放入一个变量中,该变量可用于填充电子邮件和.csv文件。

一切正常,除了我需要指向电子邮件和.csv文件中包含的每个上传文件的链接。经过几天的尝试,我似乎无法弄清楚那部分内容。

简化的HTML表单:

[RegularExpression("^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[@$!%*#?&])[A-Za-z[0-9]@$!%*#?&]{8,}$",ErrorMessage="Password must contain atleast 1 number, 1 letter, and 1 special character.")]

PHP:注意:我已经删除了所有验证/清理,删除了.csv附加以及第二封电子邮件提交。我假设一旦我们可以将链接链接到一封电子邮件中,其余的将几乎相同。

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

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

  1. 如果要将文件附加到电子邮件,则可能需要服务器上的完整路径,而不是外部URL。看一下realpath PHP函数,并尝试将保存文件的路径包装到该函数调用中,使之类似于:

    $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);
    
$filepath之后的

应该包含服务器上已上传文件的完整路径。

  1. 如果您希望在邮件中添加带有外部URL的链接,则确实需要完整的URL,但这取决于域名和上载文件夹相对于域根(/)的位置。它应该类似于http://example.com/uploads/11-16-2018-name-6534.jpg,并且您可以通过浏览器测试对生成的文件名的访问来解决这一问题,假设您可以通过网络服务器访问上载文件夹。确定网址后,您可以将域名和路径保存到配置文件中,也可以检查$_SERVER['SERVER_NAME']是否包含域名,然后在路径后附加域名。

要使用电子邮件文本中的URL,您需要修改代码,例如:

$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));