0

I am building a folder with files in it. At the end, I want to Zip this folder. On my local machine using Homestead, everything works correctly.

However, on my web server I am getting the error:

ZipArchive::close(): Can't remove file: No such file or directory

Why? The folder is filled with all files...

My code:

$zip_file = storage_path('app\\takeouts\\takeout_' . $this->dataExports->uuid . '.zip');
        $this->zip = new \ZipArchive();
        $this->zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
        $this->addAllFilesToZipArchive($this->folder_name);
        $this->zip->close();

        Storage::deleteDirectory($this->folder_name);


private function addAllFilesToZipArchive($dir)
    {
        $dirs = Storage::directories($dir);
        $files = Storage::files($dir);
        foreach ($files as $file)
        {
            if(Storage::exists(storage_path("app\\" . $file))) {
                $this->zip->addFile(storage_path("app\\" . $file), str_replace($this->folder_name,"","/" . $file));
            }
        }
        foreach ($dirs as $dir2) {
            $this->addAllFilesToZipArchive($dir2);
        }
    }
Marc.2377
  • 7,807
  • 7
  • 51
  • 95
Patrick Schocke
  • 1,493
  • 2
  • 13
  • 25

1 Answers1

0

It may seem a little obvious to some but it was an oversight on my behalf.

ll the close() function.

If the files added to the object aren't available at save time the zip file will not be created.

Marc.2377
  • 7,807
  • 7
  • 51
  • 95
Levon Babayan
  • 266
  • 2
  • 18
  • I don't get it - how can it be an oversight on your behalf, when you're not the author of the question? – Marc.2377 Jun 29 '19 at 17:26