0

I recently changed the follow upload function to put multiple files into an array, so that they would all upload. This is working well. However, now when trying to attach all of the files to an email attachment. I am just getting the string "Array" in the $filename variable.

How can I get these lines:

$fu = new fileUpload();
$filename = $fu->upload();
$template = str_replace("{filename}",

To break the array so that the attachments include all of the files being attached?

The error is: Notice: Array to string conversion in ... for the $template variable.

I have used the join function in the past to solve this, but not sure how to do so when calling a function.

if (!empty($_FILES['uploadedFile']['name']) && $_FILES['uploadedFile']['error'] != 4) {
        $fu = new fileUpload();
        $filename = $fu->upload();
        $template = str_replace("{filename}", "A file was uploaded. You can download the file from: <a href='/php/uploads/{$filename}'>{$filename}</a>", $template);
//            print_r($template);
        if ( !$filename ) {
            echo json_encode(['status_code' => 500,
                'message' => "We were not able to upload your file at this time."]);
        }
        clearstatcache();
//print_r([
//    'filename' => $target_file,
//    'file movement' => $status,
//    'is_file_uploaded' => json_encode(is_uploaded_file($_FILES['uploadedFile']['tmp_name'])),
//    ]);
} else {
    $template = str_replace("{filename}", "", $template);
}

Here is the upload function:

class fileUpload
{

    public function __construct()
    {}
    public function upload() {

        $file_count = count($_FILES['uploadedFile']['name']);
        $results = [];
        for ($i = 0; $i<$file_count; $i++) {
//          echo $file['uploadedFile']['name'][$index] . "\n";

            $target_dir = "uploads/";
            $target_file = $target_dir . basename($_FILES["uploadedFile"]["name"][$i]);
            $uploadOk = 1;
            $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));        

            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                return 0;
    // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["uploadedFile"]["tmp_name"][$i], $target_file)) {
                    $results[] = basename($_FILES["uploadedFile"]["name"][$i]);
                } else {
                    return 0;
                }
            }
        }
        return $results;
    }
}

Update - adding foreach loop:

$fu = new fileUpload();
$filename = $fu->upload();
foreach ($filename as $indFile) {
    $template = str_replace("{filename}", "A file was uploaded. You can download the file from: <a href='/php/uploads/{$indFile}'>{$indFile}</a>", $template);
}
Paul
  • 3,348
  • 5
  • 32
  • 76
  • @splash58 I was referring to this question before posting. My question is direct. I even state I know I can use the `join` function to do so, but do not know how to do it while calling a function. – Paul Aug 05 '19 at 15:55
  • foreach ($filename ..... If for one file function return string (foreach(array) $filename ... – splash58 Aug 05 '19 at 16:01
  • @splash58 Are you saying to literally just do: `$filename = (foreach(array)$filename);` and then call `$filename`? – Paul Aug 05 '19 at 16:17
  • I think you should loop array of filenames, making link for each item. – splash58 Aug 05 '19 at 16:20
  • @splash58 I'm already doing that in the upload function. So when I call it, why wouldn't there be a link for each one? – Paul Aug 05 '19 at 16:25
  • $filename = $fu->upload(); that string returns array. Next can't be done with array `{$filename}` – splash58 Aug 05 '19 at 16:31
  • @splash58 Ok, so I added a foreach loop and still only the first file is attaching. I updated my question at the bottom with the new code. – Paul Aug 05 '19 at 16:44
  • 1
    Print $template at end of each loop. I hope you will understand the problem – splash58 Aug 05 '19 at 17:03
  • @splash58 I just submitted the form with two files. When I do this: `$template = str_replace("{filename}", "A file was uploaded. You can download the file from: {$indFile}", $template); print_r($template);` I am seeing the same attachment populate for both templates in the network tab. – Paul Aug 05 '19 at 17:12
  • 1
    Something as - http://sandbox.onlinephpfunctions.com/code/afac215c7fe04497649b93615fd6be552321c761 – splash58 Aug 05 '19 at 18:01
  • @splash58 Awesome! That did it. I really appreciate your help! – Paul Aug 05 '19 at 18:24

0 Answers0