【问题标题】:How to set a dynamic path for ZipArchive addFile如何为 ZipArchive addFile 设置动态路径
【发布时间】:2019-10-02 11:47:38
【问题描述】:

编辑

问题解决了。必须更新我的文件系统配置。

我想创建一个 zip 文件,其中包含之前生成的所有发票。问题是,如果我尝试使用 laravel 存储 url()get() 函数,ZipArchive 找不到文件。

我尝试使用storage_path()->url()->get() 解决它,但它们都不起作用。它仅在我输入文件路径时才有效。

这就是它现在的样子,并且可以正常工作。

$invoiceZipFileName = Carbon::now()->toDateString() . "_invoices.zip";

$zip = new ZipArchive();
$zip->open($invoiceZipFileName, ZipArchive::CREATE);
$files->each(function ($item, $key) use ($zip, $disk) {
    $zip->addFile("storage/invoices/" . $item, $item);
});
$zip->close();

我想要实现的是这样的:

$invoiceZipFileName = Carbon::now()->toDateString() . "_invoices.zip";

$zip = new ZipArchive();
$zip->open($invoiceZipFileName, ZipArchive::CREATE);
$files->each(function ($item, $key) use ($zip, $disk) {
    $zip->addFile($disk->get($item), $item);
});
$zip->close();

或:

$invoiceZipFileName = Carbon::now()->toDateString() . "_invoices.zip";

$zip = new ZipArchive();
$zip->open($invoiceZipFileName, ZipArchive::CREATE);
$files->each(function ($item, $key) use ($zip, $disk) {
    $zip->addFile($disk->url($item), $item);
});
$zip->close();

或:

$invoiceZipFileName = Carbon::now()->toDateString() . "_invoices.zip";

$zip = new ZipArchive();
$zip->open($invoiceZipFileName, ZipArchive::CREATE);
$files->each(function ($item, $key) use ($zip, $disk) {
    $zip->addFile(storage_path("invoices") . "/" . $item, $item);
});
$zip->close();

这些是错误消息(每个都是在另一种情况下,它们不会一起出现)我得到:

exception: "Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException"
file: "C:\xampp\htdocs\invoicing\vendor\symfony\http-foundation\File\File.php"
line: 36
message: "The file "2019-10-02_invoices.zip" does not exist"
exception: "ErrorException"
file: "C:\xampp\htdocs\invoicing\app\Http\Controllers\Api\V1\InvoiceController.php"
line: 211
message: "ZipArchive::close(): Failure to create temporary file: No error"

++++ 编辑 ++++ 问题解决了。必须更新我的文件系统配置。

【问题讨论】:

    标签: laravel storage filepath ziparchive laravel-storage


    【解决方案1】:
    • 使用以下链接将 Zipper 包安装到您的 laravel 项目中。

      https://github.com/Chumper/Zipper

    • 完成 Zipper 包设置后,将以下代码用于您尊敬的控制器。

      $files = glob($image_dir_media . $file_new_name);

      $date = new DateTime();

      $zip = $date->format('Y-m-d') 。 '_invoices.zip';

      \Zipper::make(public_path() . '/media/' . $zip)->add($files)->close();

    谢谢 PHPanchal

    【讨论】:

    • 这就是为什么我要避免使用它的原因:I haven't updated this package in a long time except merging PRs. The last time I was using this package was with PHP5. I archived the repository for the reason that I am no longer working with PHP (we all have to move on sometimes) and have no time to take proper care of it anymore.
    • 你检查过项目中的更新作曲家命令吗?您在 PHP5 运行期间开发此模块,但当前 PHP7 正在运行,因此必须更新版本,并且给定的包 url 需要几分钟的时间来设置并且可以正常工作。
    • 仍然不想使用不再支持的过时软件包。
    • 是的,这对使用更新版本来说是件好事,但这个还没有过时,我们现在可以使用。 github.com/Chumper/Zipper 您将通过给定的 url 获得更多详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 2019-11-06
    相关资源
    最近更新 更多