如何为ZipArchive addFile设置动态路径

问题描述 投票:0回答:1

我想创建一个zip文件,其中包括以前生成的所有发票。问题是,如果我尝试使用Laravels存储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个回答
0
投票
  • 使用下面的链接将Zipper软件包安装到您的laravel项目中。

    https://github.com/Chumper/Zipper

  • 完成Zipper程序包安装后,将以下代码用于您尊敬的控制器中。

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

    $ zip = $ hash。 '的.zip';

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

感谢PHPanchal

© www.soinside.com 2019 - 2024. All rights reserved.