PHP ZIP函数返回损坏的文件

时间:2015-06-05 17:12:41

标签: php zip archive zipfile

我已经创建了一个脚本来备份我的服务器文件以及数据库并将它们上传到远程服务器。运行脚本时,它就像魅力和预期的那样工作。我得到一个SQL文件和一个ZIP文件。

我的问题是ZIP文件。当我将文件下载到我的PC时,我无法通过7-ZIP打开它。下面是我用来生成ZIP文件的代码:

// Get real path for our folder
$rootPath = realpath(dirname(__FILE__));
$archiveName = date("Ymd") . '-backup.zip';

// Initialize archive object
$zip = new ZipArchive();
$zip->open("./BACKUP/" . $archiveName, 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();
while(!file_exists("./BACKUP/" . $archiveName)) {}

该文件存储在由X10hosting托管的远程服务器上(如果有帮助)

0 个答案:

没有答案