I
I
Igor2018-10-02 16:10:16
PHP
Igor, 2018-10-02 16:10:16

Archiving a folder to ZIP in PHP?

Hello everyone, how to archive the contents of a folder in ZIP if "folder" is a variable with a path to the folder.
I looked at the classes for this, but I’m apparently far from the classes, and I didn’t figure out how to make a working code -_-
There was such an attempt:

<?php
require_once "ZipStream.php";

class ZipOutput {
    private $files = array();

    /**
     * @param array $files
     */
    public function setFiles($file_list)
    {
        $this->files = $file_list;
    }

    /**
     * @return array
     */
    public function sendCompressed (){
        $zip = new ZipStream("Files.zip");
        foreach ($this->files as $file)
        {
            $zip->addLargeFile($file['filepath_local'], basename($file['filepath']));
        }
        $zip->finalize();
    }
}

?>

Referencing a class with
Git Git

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Bogdan Karpov, 2018-10-02
@m0pfin

PHP has a built-in class for working with zip archives ZipArchive
look here:
php.net/manual/ru/book.zip.php
janicky.com/stati/rabota-s-zip-arhivami-v-php

<?php
 
$zip = new ZipArchive();
 
$zip->open('archive1.zip', , ZIPARCHIVE::CREATE);
$files = scandir($papka); // $papka перем. с путэм к папке
foreach($files as $file){
    if ($file == '.' || $file == '..' ){continue;}
    $f = $papka.DIRECTORY_SEPARATOR.$file;
     $zip->addFile($f);
}
$zip->close();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question