php phpMailer 附件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6322610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
phpMailer attachment
提问by Muiter
I'm using this to attach files to an mail after uploading them to my server:
在将文件上传到我的服务器后,我使用它来将文件附加到邮件中:
for ($i = 0; $i <= 2; $i++)
{
$mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['name'], $_FILES['uploaded'.$i]['name']);
}
Adding attachments is optional, but when the are no files uploaded it gives an error: Could not access file
添加附件是可选的,但是当没有上传文件时会出现错误:无法访问文件
How can I prevent this error of showing?
如何防止出现此错误?
采纳答案by Muiter
I have been able to solve this problem with using an counter:
我已经能够使用计数器解决这个问题:
$locatie = 'uploads/';
$upload_count = -1;
for ($i = 0; $i <= 2; $i++)
{
if($_FILES['uploaded'.$i]['type'] != 'application/octet-stream') // Geen php files
{
$folder = $locatie.basename($_FILES['uploaded'.$i]['name']) ;
if(move_uploaded_file($_FILES['uploaded'.$i]['tmp_name'], $folder))
{
$upload_count ++;
}
}
Loop for the attachment:
循环附件:
for ($i = 0; $i <= $upload_count; $i++)
{
$mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['name'], $_FILES['uploaded'.$i]['name']);
}
回答by hakre
See here how to deal with file uploads first:
先看这里如何处理文件上传:
You need to refer to the temporary filename. That's needed to get the actual filename, not only the name of the file.
您需要参考临时文件名。这是获取实际文件名所必需的,而不仅仅是文件名。
$_FILES['userfile']['tmp_name']
So access $_FILES['userfile']['name']
for the written filename of the attachment and $_FILES['userfile']['tmp_name']
to specify the actual file (the data) of the upload.
因此访问$_FILES['userfile']['name']
附件的书面文件名并$_FILES['userfile']['tmp_name']
指定上传的实际文件(数据)。
Roughly put into your code, incl. a validationyou should do as well to verify it's actually a file-upload:
粗略地放入你的代码中,包括。验证你应该做的一样好,以验证它实际上是一个文件上传:
for ($i = 0; $i <= 2; $i++)
{
# ignore file that have not been uploaded
if (empty($_FILES['uploaded'.$i])) continue;
# get the data of the file
$fileName = $_FILES['uploaded'.$i]['name'];
$filePath = $_FILES['uploaded'.$i]['tmpname'];
# add only if the file is an upload
is_uploaded_file($filePath)
&& $mail->AddAttachment($filePath, $fileName)
;
}
A word of caution
一句小心的话
Your code mixes two jobs with each other. That makes it hard for you to debug and improve - and to take care of things like file/system issues and security.
您的代码将两个工作相互混合。这使您难以调试和改进 - 以及处理文件/系统问题和安全性等问题。
I suggest you a little different approach: Do one step after the other. In your case namely, 1.) process the file uploads and gather the data you need, 2.) add these attachments.
我建议你采用一种不同的方法:一步一步地做。在您的情况下,1.) 处理文件上传并收集您需要的数据,2.) 添加这些附件。
You can improve the first part by taking a look into the PHP Manual. If you want to support the upload of multiple files, I suggest you orient yourself on the suggestions given in the page Uploading multiple files. Then process the file uploads and form an array that contains the filename on the client computer and the path on the server system per entry.
您可以通过查看 PHP 手册来改进第一部分。如果您想支持多个文件的上传,我建议您根据上传多个文件页面中给出的建议来定位自己。然后处理文件上传并形成一个数组,其中包含客户端计算机上的文件名和服务器系统上每个条目的路径。
// see PHP Manual for multi file uploads, this is based on it
$validAttachments = array();
foreach($_FILES['userfile']['name'] as $index => $fileName)
{
$filePath = $_FILES['userfile']['tmp_name'][$index];
if(is_uploaded_file($filePath))
{
$attachment = new stdClass;
$attachment->fileName = $fileName;
$attachment->filePath = $filePath;
$validAttachments[] = $attachment;
}
}
If there is an error already in that part, you know that it's related to the file upload procedure. This is untested code, so just illustrating a direction.
如果该部分已经存在错误,则您知道它与文件上传过程有关。这是未经测试的代码,所以只是说明一个方向。
In the second step you can then just iterate over such an array and add the attachments:
在第二步中,您可以遍历这样的数组并添加附件:
foreach($validAttachments as $attachment)
{
$mail->AddAttachment($attachment->filePath, $attachment->fileName);
}
You can then better check for errors in the different parts without mixing one problem with the other.
然后,您可以更好地检查不同部分中的错误,而不会将一个问题与另一个问题混淆。
回答by patapizza
Before adding an attachment, you should check if the uploaded file exists, e.g. with fopen
.
在添加附件之前,您应该检查上传的文件是否存在,例如带有fopen
.
回答by Shef
Solution
解决方案
for ($i = 0; $i <= 2; $i++)
{
if (file_exists($locatie.$_FILES['uploaded'.$i]['tmp_name'])) {
$mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['tmp_name'], $_FILES['uploaded'.$i]['name']);
}
}
Update
更新
You were using $locatie.$_FILES['uploaded'.$i]['name']
instead of $locatie.$_FILES['uploaded'.$i]['tmp_name']
as the file to load. When a file is uploaded it is renamed with a temporary name and placed on the temporary folder. That's where you will get it, and that's why you need to reference it with $locatie.$_FILES['uploaded'.$i]['tmp_name']
您正在使用$locatie.$_FILES['uploaded'.$i]['name']
而不是$locatie.$_FILES['uploaded'.$i]['tmp_name']
作为要加载的文件。上传文件后,将使用临时名称对其进行重命名并放置在临时文件夹中。这就是你会得到它的地方,这就是为什么你需要引用它$locatie.$_FILES['uploaded'.$i]['tmp_name']
回答by Supamic
Not sure if this does exactly what you want but this is how I attached multiple uploaded files and kept original filenames from a webform. Error handling/messaging for user will not give error for UPLOAD_ERR_NO_FILE because file uploads were optional fields but still processes each input field in the foreach loop.
不确定这是否完全符合您的要求,但这就是我附加多个上传文件并从网络表单中保留原始文件名的方式。用户的错误处理/消息传递不会为 UPLOAD_ERR_NO_FILE 提供错误,因为文件上传是可选字段,但仍会处理 foreach 循环中的每个输入字段。
foreach ($_FILES["userfile"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["userfile"]["tmp_name"][$key];
$name = $_FILES["userfile"]["name"][$key];
$mailer->AddAttachment($tmp_name, $name);
}
$name = $_FILES["userfile"]["name"][$key];
switch($error){
case UPLOAD_ERR_INI_SIZE: echo $errmsg1.$name.$errmsg2;
break;
case UPLOAD_ERR_FORM_SIZE: echo $errmsg1.$name.$errmsg2;
break;
case UPLOAD_ERR_PARTIAL: echo $errmsg1.$name.$errmsg2;
break;
case UPLOAD_ERR_NO_FILE:
break;
case UPLOAD_ERR_NO_TMP_DIR: echo $errmsg1.$name.$errmsg2;
break;
case UPLOAD_ERR_CANT_WRITE: echo $errmsg1.$name.$errmsg2;
break;
case UPLOAD_ERR_EXTENSION: echo $errmsg1.$name.$errmsg2;
break;
}
}
回答by konsolenfreddy
Agreeing with @patapizza, but before you check on the filesystem (which you should do anyway), you can also just check for $_FILES like
同意@patapizza,但在检查文件系统之前(无论如何您都应该这样做),您也可以只检查 $_FILES 之类的
for ($i = 0; $i < count($_FILES); $i++){
// only iterate over the number of files you actually have
}
回答by LazyOne
You need to check if that file was actually uploaded/exists before adding the attachment. Something like
在添加附件之前,您需要检查该文件是否实际上传/存在。就像是
for ($i = 0; $i <= 2; $i++)
{
if (file_exists($locatie.$_FILES['uploaded'.$i]['tmp_name'])) {
$mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['tmp_name'], $_FILES['uploaded'.$i]['name']);
}
}