Quantcast
Channel: How to set a dynamic path for ZipArchive addFile - Stack Overflow
Viewing all articles
Browse latest Browse all 2

How to set a dynamic path for ZipArchive addFile

$
0
0

EDIT

Problem solved. Had to update my filesystems config.

I want to create a zip file including all invoices which have been generated before. The problem is, if I am trying to do it with laravels storage url() or get() function, ZipArchive can not find the files.

I tried to solve it with storage_path(), ->url(), ->get() but none of them worked. It only works if I am typing the path to the files.

This is how it looks right now and it works.

$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();

What I want to achieve is something like that:

$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();

or:

$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();

or:

$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();

These are the error messages (each was in another case, they don't occur together) I get:

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"

++++ EDIT ++++ Problem solved. Had to update my filesystems config.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images