Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress a folder in yii2 web app into a zip?

Tags:

php

zip

yii2

I am trying to zip a folder.

This is the function in ExportarController.php

public function zipping(){
        $rootPath = realpath('results/');

        // Initialize archive object
        $zip = new \ZipArchive();
        $zip->open('../web/descargas/Region.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

        // Initialize empty "delete list"
        $filesToDelete = array();

        // Create recursive directory iterator
        /** @var SplFileInfo[] $files */
        $files = new RecursiveIteratorIterator(
            new RecursiveDirectoryIterator($rootPath),
            RecursiveIteratorIterator::LEAVES_ONLY
        );

        foreach ($files as $name => $file)
        {
            // Skip directories (they would be added automatically)
            if (!$file->isDir())
            {
                // Get real and relative path for current file
                $filePath = $file->getRealPath();
                $relativePath = substr($filePath, strlen($rootPath) + 1);

                // Add current file to archive
                $zip->addFile($filePath, $relativePath);

                // Add current file to "delete list"
                // delete it later cause ZipArchive create archive only after calling close function and ZipArchive lock files until archive created)
                if ($file->getFilename() != 'important.txt')
                {
                    $filesToDelete[] = $filePath;
                }
            }
        }

        // Zip archive will be created only after closing object
        $zip->close();

        // Delete all files from "delete list"
        foreach ($filesToDelete as $file)
        {
            unlink($file);
        }

    }

It worked pefectly when I tried it outside yii2 webapp. But when I added the function to yii2 controller I got the error:

Class 'app\controllers\ZipArchive' not found

Do you know what is going wrong? does it have something to do with the directory I'm trying to write in?

Please help me.

Thank you so much.

SOLUTION

It was cuz of the namespace, I had to ad \ to every ziparchive function.

Like \ZipArchive() and \ZipArchive::CREATE | \ZipArchive::OVERWRITE

Thanks to all of you.

like image 547
Maurocrispin Avatar asked Dec 11 '25 06:12

Maurocrispin


1 Answers

First enable php zip extension

$zip = new \ZipArchive();

                $test = tempnam(sys_get_temp_dir(), rand(0, 999999999) . '.zip');
                var_dump($test);
                $res = $zip->open($test, \ZipArchive::CREATE);

                if ($res) {
                    foreach ($csv_data as $data) {

                        $zip->addFile($data['file_name'],'chat_'. $data['chatID'] .'.csv');

                    }
                    $zip->close();
                    header('Content-Type: application/zip');
                    header('Content-Disposition: attachment; filename=chat_' . date('Ymd_His'). '.zip');
                    readfile($test);

                } else {
                    echo 'zip error';
                    die;
                }

Read for More : http://php.net/manual/en/class.ziparchive.php

like image 130
Bharoo Jangid Avatar answered Dec 12 '25 20:12

Bharoo Jangid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!