Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

php - phpMailer attachment

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See here how to deal with file uploads first:

Handling file uploads

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.

Roughly put into your code, incl. a validation you 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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...