Создание zip всех каталогов и файлов в одном формате
Я пытаюсь сделать zip массива файлов в том же формате, что и на моем компьютере, скрипт, который я написал, работает, единственная проблема заключается в том, что он перезаписывает первый элемент каталога, а второй - только последний элемент. в массиве каталогов идет в zip-файле, мне нужно добавить все файлы из предоставленного массива каталогов. Вот мой код, пожалуйста, скажите мне, что я делаю не так.
// Get real path for our folder
$rootPaths = array(
'D:\xampp\htdocs\moko\wp-content\plugins\moko\classes',
'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates',
);
$valid_files = array();
if (is_array($rootPaths)) {
foreach ($rootPaths as $new_files) {
if (file_exists($new_files)) {
$valid_files[] = $new_files;
}
}
}
// Initialize archive object
if (count($valid_files)) {
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
foreach ($valid_files as $rootPath) {
$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();
}
Этот скрипт создает только zip-файл с файлами и структурой каталогов следующей директории.
'D: \ XAMPP \ HTDOCS \ Моко \ Wp-контента \ Plugins\ Моко \ Шаблоны'
1 ответ
Вам не нужно создавать переменную $valid_files
совсем. И цикл, о котором вы говорите, состоит в том, чтобы проверять допустимые файлы в каталогах, только проверяет, что каталоги в $rootPaths
переменная существует или нет. Также я изменил способ добавления файлов в zip. Цикл переписывался $files
переменная каждый раз при исполнении. Посмотрите на следующий код.
// Get real path for our folder
$rootPaths = array(
'D:\xampp\htdocs\moko\wp-content\plugins\moko\classes',
'D:\xampp\htdocs\moko\wp-content\plugins\moko\templates',
);
if (is_array($rootPaths)) {
foreach ($rootPaths as $key => $new_files) {
if (!file_exists($new_files)) {
unset($rootPaths[$key]);
}
}
}
// Initialize archive object
if (count($rootPaths)) {
$zip = new ZipArchive();
$zip->open('file.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
foreach ($rootPaths as $rootPath) {
$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();
}