phpMailer attachment

前端 未结 7 1928
时光取名叫无心
时光取名叫无心 2020-12-06 08:55

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


        
7条回答
  •  春和景丽
    2020-12-06 09:25

    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.

提交回复
热议问题