Я пытаюсь создать zip-вложение из только что загруженных файлов. Чтобы подтвердить, файлы, которые я пытаюсь загрузить, загружаются. Проблема изолирована от почтового индекса и кода ниже.
Это ошибка, которую я получаю:
Предупреждение : ZipArchive :: close (): Невозможно удалить файл: Нет такого файла или каталога в
Для этой строки кода:
$f->close();
Кто-нибудь видит, что я делаю неправильно?
Вот полный код:
$date = new DateTime();
$fu = new fileUpload();
$filename = $fu->upload();
$uploadedFileTypes = $fu->getImageFileTypes();
$extensions = ['pdf','jpg', 'jpeg', 'png', 'gif'];
$differentExtensions = array_diff($uploadedFileTypes, $extensions);
if (count($differentExtensions) > 0) {
$f = new ZipArchive();
$zip = $f->open('uploads/' . $_FILES['uploadedFile']['name'][0] . ".zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($zip) {
for ($index = 0; $index < count($_FILES['uploadedFile']['name']); $index++) {
$f->addFile($_FILES['uploadedFile']['tmp_name'][$index], $_FILES['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");
}
Fileupload Class:
class fileUpload
{
public function __construct()
{}
public $imageFileTypes = [];
public function getImageFileTypes()
{
return $this->imageFileTypes;
}
public function upload() {
if (!empty($_FILES["uploadedFile"]["name"])) {
$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));
// here's the important bit
$this->imageFileTypes[] = $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;
Полный код файла, на котором выполняется попытка архивирования:
$files = null;
// print_r($_FILES);
if (!empty($_FILES["uploadedFile"]["name"])) {
if ($_FILES['uploadedFile']['error'] == 1) {
$error = "The file {$_POST['first_name']} attempted to upload is too large. Contact them for an alternate way to send this file.";
$template = str_replace("{filename}", "$error", $template);
}
$date = new DateTime();
$fu = new fileUpload();
$filename = $fu->upload();
$uploadedFileTypes = $fu->getImageFileTypes();
//$fileTypeString = implode( ", ", $uploadedFileTypes);
$extensions = ['pdf','jpg', 'jpeg', 'png', 'gif'];
//file_put_contents('file_type_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r($uploadedFileTypes, true), FILE_APPEND);
$differentExtensions = array_diff($uploadedFileTypes, $extensions);
if (count($differentExtensions) > 0) {
file_put_contents('file_norm_log', "\n[{$date->format('Y-m-d H:i:s')}]" . print_r('There were other types of files uploaded', true), FILE_APPEND);
$f = new ZipArchive();
$zip = $f->open('uploads/' . $_FILES['uploadedFile']['name'][0] . ".zip", ZipArchive::CREATE | ZipArchive::OVERWRITE);
//var_dump($f);
//var_dump($zip);
if ($zip) {
for ($index = 0; $index < count($_FILES['uploadedFile']['name']); $index++) {
// echo $file['uploadedFile']['name'][$index] . "\n";
$f->addFile($_FILES['uploadedFile']['tmp_name'][$index], $_FILES['uploadedFile']['name'][$index]);
}
$check = $f->addFile($_FILES['uploadedFile']['tmp_name'][$index], $_FILES['uploadedFile']['name'][$index]);
var_dump($check);
$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.");
}
} else {
$out = (count($filename) > 1 ? 'Multiple files were' : 'A file was'). ' uploaded. You can download ' . (count($filename) > 1 ? 'them' : 'the file'). ' from:</ul>';
foreach ($filename as $indFile) {
//print_r($template);
$out .= "<li><a href='/php/uploads/{$indFile}'>{$indFile}</a></li>";
}
$out .= '</ul>';
$template = str_replace("{filename}", $out, $template);
}
foreach ($_FILES as $file) {
foreach($file['name'] as $key => $value) {
if (empty($value)) {
//echo "name is empty!";
$template = str_replace("{filename}", '', $template);
}
if ($file['error'][$key] != 4) {
//echo "error code is 4";
}
}
}
clearstatcache();
}