Сжатие файлов в zip через PHP - PullRequest
0 голосов
/ 31 октября 2019

Я пытаюсь создать php-файл, который сжимает все файлы в папке на веб-сайте на CPanel. К сожалению, это не работает.

Где я делаю неправильно? Есть ли у вас какие-либо советы?

// Get real path for our folder
$rootPath = realpath('/home/username/public_html');

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

// 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);
    }
}

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