• Hi !

    I created a custom plugin very simple with just a button that create a ZIP file of a folder when is clicked.
    Here is my code when the button is clicked :

    if(isset($_POST['Submit'])) {
            $rootPath = realpath('../wp-content/uploads/users/photo');
            $zip = new ZipArchive();
            $zip->open('Utilisateurs.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
    
            $files = new RecursiveIteratorIterator(
                new RecursiveDirectoryIterator($rootPath),
                RecursiveIteratorIterator::LEAVES_ONLY
            );
    
            foreach ($files as $name => $file)
            {
    
                if (!$file->isDir())
                {
                    $filePath = $file->getRealPath();
                    $relativePath = substr($filePath, strlen($rootPath) + 1);
                    $zip->addFile($filePath, $relativePath);
                }
            }
    
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header("Content-type: application/octet-stream");
            header("Content-Disposition: attachment; filename=\"Utilisateurs.zip\"");
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: ".filesize("Utilisateurs.zip"));
            ob_end_flush();
            readfile("Utilisateurs.zip");
        }

    My problem is : the ZIP file is created correctly and I download it automatically but the downloaded version is empty. This problem appears only when I execute the code from the plugin, when I try to execute it from an other place it works perfectly the ZIP file contains all elements. Someone knows why or how to fix this problem?

    Thank you !

The topic ‘Empty Zip file downloaded in custom plugin’ is closed to new replies.