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