概述: 我有一个包含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>
非常感谢您的帮助!
答案 0 :(得分: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
之后的应该包含服务器上已上传文件的完整路径。
$_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));