6

need some help for laravel zip file. i have a folder in the public folder and i would like to create a zip of that folder (temp_file folder) whenever the user clicks the button.

public function testing()
{
    $public_dir = public_path('temp_file/');
    $zipFileName = 'myZip.zip';
    $zip = new ZipArchive;

    if ($zip->open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) {
        $zip->addFile('file_path', 'file_name');
        $zip->close();
    }

    $headers = array('Content-Type' => 'application/octet-stream');

    $filetopath = $public_dir . '/' . $zipFileName; 
}

but it seems that it is not creating the zip file and i can't download it. Please need some help

Zayn Ali
  • 4,765
  • 1
  • 30
  • 40
needhelp
  • 107
  • 1
  • 2
  • 5
  • 1
    According to your example, the zip-file should be empty, because you're adding non-existent files: `$zip->addFile('file_path','file_name');` and I can't see any point, where you stream the file to the client. You just define the path to the empty zip file. – Dan Aug 02 '17 at 01:54
  • i have change it to $zip->addFile($public_dir,$zipFileName); but i m getting error ZipArchive::close(): Read error: Is a directory – needhelp Aug 02 '17 at 02:02
  • The first parameter is the path to the file, the second parameter is the name you want inside the zip file. – Scuzzy Aug 02 '17 at 02:06
  • Create a file download response like `return response()->download($filetopath, $zipFileName, $headers);` – ASR Aug 02 '17 at 02:28
  • but it is not creating the zip file – needhelp Aug 02 '17 at 02:37
  • Can you update the code above to your current working status? – Dan Aug 02 '17 at 02:44
  • Does the folder `temp_files` have the right permissions to save a file? – Dan Aug 02 '17 at 02:46
  • please check my answer. it may help – Bhavin Solanki Aug 02 '17 at 05:13

3 Answers3

10

Many people may not know this but ZipArchive::addFile() and ZipArchive::close() also return a boolean to show their success (or failure). You should always check them because only the close method returns if the folder isn't writeable.

Then you said that nothing downloads if you call the controller action. That's right. You didn't tell the program to stream something to the client. You just set two variables, one for headers? and the other one for the exact same file path used above to open the zip file.

The following code is a working example (at least in a configured environment with correct folder permissions) how this procedure can work and get you some "inspiration" for your task.

public function testing()
{
    // Create a list of files that should be added to the archive.
    $files = glob(storage_path("app/images/*.jpg"));

    // Define the name of the archive and create a new ZipArchive instance.
    $archiveFile = storage_path("app/downloads/files.zip");
    $archive = new ZipArchive();

    // Check if the archive could be created.
    if (! $archive->open($archiveFile, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
        throw new Exception("Zip file could not be created: ".$archive->getStatusString());
    }

    // Loop through all the files and add them to the archive.
    foreach ($files as $file) {
        if (! $archive->addFile($file, basename($file))) {
            throw new Exception("File [`{$file}`] could not be added to the zip file: ".$archive->getStatusString());
        }
    }

    // Close the archive.
    if (! $archive->close()) {
        throw new Exception("Could not close zip file: ".$archive->getStatusString());
    }
    
    // Archive is now downloadable ...
    return response()->download($archiveFile, basename($archiveFile))->deleteFileAfterSend(true);
}
Dan
  • 5,140
  • 2
  • 15
  • 30
0

If everything works for you, you need to check the permission of each directory at public_path(). The directory must have 0755 or 0777 permission to create and execute files. Please check your directory permission first.

Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46
-1

You have to do in Popper format .
Do Like this

$File = 'path-ZipFile/fileName' . time() . '.zip';
$zip = new \ZipArchive();
$res = $zip->open($File, \ZipArchive::CREATE);

if ($res === TRUE) {
    $zip->addFile("fullpath/filename.extinction");
    $zip->close();
}

header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Length: " . filesize($File));
header("Pragma: no-cache");
header("Expires: 0");
readfile($File);
header("Connection: close");
Bibhudatta Sahoo
  • 4,808
  • 2
  • 27
  • 51