4

Im using Laravel 5.2 and Zipper to create ZIP archive in a fly and download it by user. I think this problem is generall not strictly related to Laravel or Zipper. Steps:

  1. User click link download_all.
  2. First php script create archive.
  3. Next the same script push created file to force user download.

Everything sounds normal but I have strange behaviour that after zip archive is created (point 2) php/server cant see this newly created file. Both filesize and file_exists on $filePath return false but file exist. I cant read file why?

When I redirect user to $filePath (instead of reading it and pushing to download) everything is okay and user get file. But why I cant access newly creatd file during "script lifetime"? $paths are correct. Tested on Windows 7 and Unix.

Any idea?

code:

public function downloadAll($id)
    {
        $sourceFilesDir   = 'upload/source/' . $id;
        $zipPath          = 'upload/source-zip/' . date('Y-m-d-H-i-s') . '.zip';

        Zipper::make($zipPath)->add($sourceFilesDir);

        $fullPath = public_path($zipPath);

        // works
        // return response()->redirectTo($zipPath);

        $headers = [
            'Content-Type: application/zip',
            'Content-Transfer-Encoding: Binary',
            'Content-Length: ' . filesize($fullPath),
        ];

        // dont works, cant see file
        return response()->download($fullPath, basename($zipPath), $headers);
    }
Panup Pong
  • 1,871
  • 2
  • 22
  • 44
Adiasz
  • 1,624
  • 1
  • 12
  • 21

1 Answers1

1

Soved by @Holger. Zipper should be closed to save file.

Proper code:

Zipper::make($zipPath)->add($sourceFilesDir)->close();

->close()

Unfortunately in Zipper docs this is not clearly mentioned.

Adiasz
  • 1,624
  • 1
  • 12
  • 21