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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

Before adding an attachment, you should check if the uploaded file exists, e.g. with fopen.

Solution 3

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

Solution 4

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']);
}

Solution 5

Agreeing with @patapizza, but before you check on the filesystem (which you should do anyway), you can also just check for $_FILES like

for ($i = 0; $i < count($_FILES); $i++){
    // only iterate over the number of files you actually have
}

Solution 6

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']);
    }
}

Solution 7

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.

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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply