PHP cannot access newly created zip file

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

  • User click the download_all link.
  • The first php script to create an archive.
  • Next, the same script, click the created file to force the user to load.

Everything sounds fine, but I have a strange behavior that after creating a zip archive (point 2) php / server does not see this newly created file . Both filesize and file_exists in $ filePath return false, but the file exists. I can not read the file why?

When I redirect the user to $ filePath (instead of reading it and clicking to download), everything is fine and the user receives the file. But why can't I access the newly created file during the "script lifetime"? $ paths are correct. Tested on Windows 7 and Unix.

Any idea?

the 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);
    }
+4
source share
1 answer

Compatible @ Holger. To save the file, the zipper must be closed .

The correct code is:

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

→ Close ()

Unfortunately, this is not explicitly mentioned in the Zipper docs.

+1
source

Source: https://habr.com/ru/post/1662147/


All Articles