I am running into a scenario that I cannot get more than one file to upload. If I add more than one file, only the last one will send through. I know this means I need to put the files into an array, but when I try I get an error from the fileUpload class.
IF I change the input html's name to `name="uploadedFile[]"' then I get the following error in my fileUpload class:
PHP Warning: basename() expects parameter 1 to be string, array given in /php/fileUpload.php on line 11
PHP Warning: move_uploaded_file() expects parameter 1 to be string, array given in /php/fileUpload.php on line 46
Then in my Communication class, you will see that if there is more than one file being uploaded, I send it as a zip, so I'm not sure if anything would need to be changed in there.
Does anyone see what I need to do to get the array to work?
HTML:
<input type="file" name="uploadedFile" class="inputfile" id="uploadedFileTest" data-multiple-caption="{count} files selected" multiple>
FileUpload class:
class fileUpload
{
public function __construct()
{}
public function upload() {
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["uploadedFile"]["name"]);
$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"], $target_file)) {
return basename($_FILES["uploadedFile"]["name"]);
} else {
return 0;
}
}
}
}
Rest of the php file:
$files = null;
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='https://mbkit.com/php/uploads/{$filename}'>{$filename}</a>", $template);
clearstatcache();
}
Communication Class - what sends out the attachment(s):
if (!empty($file) && !$recipient && count($file['uploadedFile']['name']) > 1) {
$f = new ZipArchive();
$zip = $f->open('uploads/' . $file['uploadedFile']['name'][0] . ".zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($zip) {
for ($index = 0; $index < count($file['uploadedFile']['name']); $index++) {
// echo $file['uploadedFile']['name'][$index] . "
";
$f->addFile($file['uploadedFile']['tmp_name'][$index], $file['uploadedFile']['name'][$index]);
}
$f->close();
$message["attachment[0]"] = curl_file_create("uploads/{$file['uploadedFile']['name'][0]}.zip",
pathinfo("uploads/{$file['uploadedFile']['name'][0]}.zip", PATHINFO_EXTENSION),
$file['uploadedFile']['name'][0] . ".zip");
} else {
throw new Exception("Could not zip the files.");
}
// $message['html'] = str_replace("{filename}", "A file was uploaded. You can download the file from: <a href='https://mbkit.com/php/uploads/{$file['uploadedFile']['name']}.zip'>{$file['uploadedFile']['name']}</a>", $message['html']);
} elseif (count($file['uploadedFile']['name']) == 1) {
$message["attachment[0]"] = curl_file_create("uploads/{$file['uploadedFile']['name']}",
pathinfo("uploads/{$file['uploadedFile']['name']}", PATHINFO_EXTENSION),
$file['uploadedFile']['name']);
}