让 ZipArchive::close() 运行并创建一个 zip 文件夹的问题 php

Issue getting ZipArchive::close() to function and a zip folder to be created php

我正在尝试从刚刚上传的文件创建一个 zip 附件。为确认,我尝试上传的文件正在上传。该问题仅存在于 zip 部分和下面的代码中。

这是我收到的错误:

Warning: ZipArchive::close(): Can't remove file: No such file or directory in

对于这行代码:

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

文件上传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();
}

正如我在评论中提到的,您似乎正在将上传的文件移动到 $fu->upload() 调用中的 uploads/ 目录,但是当您压缩它们时,您引用的是 'old' 使用 $_FILES['uploadedFile']['tmp_name'] 数组作为文件的位置。

在您的脚本中更改:

    $f->addFile($_FILES['uploadedFile']['tmp_name'][$index], $_FILES['uploadedFile']['name'][$index]);

    $f->addFile('uploads/'.basename($_FILES["uploadedFile"]["name"][$index], basename($_FILES["uploadedFile"]["name"][$index]);