I want to create a zip folder that includes an xml file and download it. I can download zip but it is empty.
I can create xml and zip :
By the way, $result is the result of ArrayToXml::convert(array,....)
$zip = new ZipArchive;
$fileName = 'example-'.time().'.xml';
$zipName = 'example'.time().'.zip';
if ($zip->open(public_path("storage/zips/".$zipName), ZipArchive::CREATE) === TRUE)
{
Storage::disk('public')->put('/files/'.$fileName, $result);
$zip->addFile(public_path("storage/files/".$fileName), $result);
Storage::disk('public')->put('/zips/'.$zipName, $zip);
$zip->close();
}
return response()->download(storage_path('app\public\zips\\'.$zipName));
How to add xml file to zip. I am new to laravel please help
My Answer:
You are almost there, but there is a small mistake in your code. Instead of using `$zip->addFile()` to add the XML file to the zip archive, you should use `$zip->addFile()` to add the XML file to the zip archive. Here is the corrected code:
php
$zip = new ZipArchive;
$fileName = 'example-'.time().'.xml';
$zipName = 'example'.time().'.zip';
if ($zip->open(public_path("storage/zips/".$zipName), ZipArchive::CREATE) === TRUE)
{
Storage::disk('public')->put('/files/'.$fileName, $result);
$zip->addFile(public_path("storage/files/".$fileName), $fileName); // Use $fileName instead of $result
Storage::disk('public')->put('/zips/'.$zipName, $zip);
$zip->close();
}
return response()->download(storage_path('app\public\zips\\'.$zipName));
By using `$fileName` instead of `$result` in the `addFile()` method, you are telling the ZipArchive to add the XML file to the zip archive with the correct file name. This should solve the issue of the zip file being empty.
Rate this post
5 of 5 based on 5865 votesComments