I have a function to download a zip file with or without folders based on the file_type_id. However when file_type_id is 1 my server is returning a empty 200 response. When i look at the temp folder i created the zip file is there and all the files are in it when i unzip it in the command line. So my question would be why this behaviour exists and if someone got a solution? The there is also nothing in the laravel.log or nginx/apache logs.
public function downloadZip($file_type, $resId)
{
$reservation = Reservation::with('contact.insurer')->where('id', $resId)->first();
if (Auth::user()->hasRole('verzekeraar')) {
$contact = Auth::user()->userable;
if ($contact->insurer_id != $reservation->contact->insurer_id) {
return AppJsonResponse::UnAuthorized('U heeft geen rechten voor dit dossier.');
}
}
$files = [];
$dirs = [];
if ($file_type == 2) {
$files = $reservation->files;
} else {
$dirs = Folder::whereNull('parent_id')->where('reservation_id', $reservation->id)->get();
$files = File::whereNull('parent_id')->where('reservation_id', $reservation->id)->where('file_type_id', 1)->get();
}
$zip_file = storage_path('app/tmp/wrapper.zip');
// Ensure tmp directory exists
if (!file_exists(storage_path('app/tmp'))) {
mkdir(storage_path('app/tmp'), 0755, true);
}
// Initializing PHP class
$zip = new ZipArchive();
if ($zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
Log::error("Cannot open <$zipPath>n");
return AppJsonResponse::Error('ZIP creation failed.');
}
if ($file_type == 2) {
foreach ($files as $file) {
if ($file->file_type_id == $file_type) {
$zip->addFile(Storage::disk('private')->getDriver()->getAdapter()->applyPathPrefix($file->path), 'storage/' . $file->name . '.' . strtolower($file->ext));
}
}
} else {
$ultPath = 'storage/';
$this->zipDocuments($dirs, $files, $ultPath, $reservation->id, $zip);
}
if (!$zip->close()) {
Log::error("Failed to close the zip file.");
} else {
Log::debug("Successfully closed zip file: $zip_file");
}
if (file_exists($zip_file)) {
Log::info("File size of ZIP: " . filesize($zip_file));
return response()->download($zip_file, 'wrapper.zip', [
'Content-Type' => 'application/zip',
]);
} else{
Log::error('ZIP file is missing or too small: ' . $zip_file);
return AppJsonResponse::Error('ZIP creation failed.');
}
}
And this is the zipdocuments function
public function zipDocuments($dirs, $files, $ultPath, $resId, $zip)
{
foreach ($dirs as $dir) {
$currentPath = $ultPath . $dir->name . '/';
// Log current directory
Log::debug("Entering directory: $currentPath");
// Add all files in current directory
$filesInDir = File::where('parent_id', $dir->id)
->where('reservation_id', $resId)
->where('file_type_id', 1)
->get();
foreach ($filesInDir as $file) {
$fullPath = Storage::disk('private')->getDriver()->getAdapter()->applyPathPrefix($file->path);
if (!file_exists($fullPath)) {
Log::warning("File not found: $fullPath");
continue;
}
if($zip->addFile($fullPath, $currentPath . $file->name . '.' . strtolower($file->ext)) === true){
}
}
// Recursively process subfolders
$subDirs = Folder::where('parent_id', $dir->id)->where('reservation_id', $resId)->get();
$this->zipDocuments($subDirs, [], $currentPath, $resId, $zip);
}
// Add top-level files (only for first call)
foreach ($files as $file) {
$fullPath = Storage::disk('private')->getDriver()->getAdapter()->applyPathPrefix($file->path);
if (!file_exists($fullPath)) {
Log::warning("Top-level file not found: $fullPath");
continue;
}
$zip->addFile($fullPath, $ultPath . $file->name . '.' . strtolower($file->ext));
}
return $zip;
}