FAQ
•
Поиск
Вход
•
Регистрация
Список форумов newssoft.ru
->
PHP
Ответить
Имя
Тема
Сообщение
Смайлики
Дополнительные смайлики
Цвет шрифта:
По умолчанию
Тёмно-красный
Красный
Оранжевый
Коричневый
Жёлтый
Зелёный
Оливковый
Голубой
Синий
Тёмно-синий
Индиго
Фиолетовый
Белый
Чёрный
Размер шрифта:
Размер шрифта
Очень маленький
Маленький
Обычный
Большой
Огромный
Закрыть теги
[quote="admin"][u]Zip a whole folder:[/u] [code] // Get real path for our folder $rootPath = realpath('folder-to-zip'); // 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(); [/code] [u]Zip a whole folder + delete all files except "important.txt":[/u] [code] // Get real path for our folder $rootPath = realpath('folder-to-zip'); // Initialize archive object $zip = new ZipArchive(); $zip->open('file.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; } } [/code] [url]https://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php[/url][/quote]
Настройки
HTML
ВЫКЛЮЧЕН
BBCode
ВКЛЮЧЕН
Смайлики
ВКЛЮЧЕНЫ
Отключить в этом сообщении BBCode
Отключить в этом сообщении смайлики
Часовой пояс: GMT + 3
Перейти:
Выберите форум
Сервера linux
----------------
Серверные команды
english support
План работ
----------------
В работе
Разработка
----------------
PHP
JS
HTML5 и CSS3
Perl и Ruby
Обзор темы
Автор
Сообщение
admin
Добавлено: Вт Июл 24, 2018 8:56 am
Заголовок сообщения: Архивирование папки в PHP
Zip a whole folder:
Код:
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// 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();
Zip a whole folder + delete all files except "important.txt":
Код:
// Get real path for our folder
$rootPath = realpath('folder-to-zip');
// Initialize archive object
$zip = new ZipArchive();
$zip->open('file.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;
}
}
https://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php
Powered by
phpBB
© 2001, 2005 phpBB Group
AllSat 22 -
photographe de mariage
- 2006