K
K
kauffun2018-04-15 11:28:24
PHP
kauffun, 2018-04-15 11:28:24

How to put into the archive not only files, but also folders inside the directory?

The essence of the question is in the title. I have two functions, one deletes everything that is inside the directory ( all folders with all files ), and the other archives all files ( but not folders and this is the problem ) and gives it to the jump, so how can I put folders in the archive?
Below is the code of the functions ( deletion function is purely for an example, it may come in handy for someone ): Deletion function
:

function myscandir($dir){
    $list = scandir($dir);
    unset($list[0],$list[1]);
    return array_values($list);
}
function clear_dir($dir){
$list = myscandir($dir);
foreach ($list as $file){
if (is_dir($dir.$file)){
clear_dir($dir.$file.'/');
rmdir($dir.$file);
}else{
unlink($dir.$file);
}
}
}
clear_dir('./');

Functions for archiving and downloads:
$pathdir='./'; // путь к папке, файлы которой будем архивировать
$nameArhive = 'test.zip'; //название архива
$zip = new ZipArchive; // класс для работы с архивами
if ($zip -> open($nameArhive, ZipArchive::CREATE) === TRUE){ // создаем архив, если все прошло удачно продолжаем
    $dir = opendir($pathdir); // открываем папку с файлами
    while( $file = readdir($dir)){ // перебираем все файлы из нашей папки
            if (is_file($pathdir.$file)){ // проверяем файл ли мы взяли из папки
                $zip -> addFile($pathdir.$file, $file); // и архивируем
                echo("Заархивирован: " . $pathdir.$file) , '<br/>';
            }
    }
    $zip -> close(); // закрываем архив.
    echo 'Архив успешно создан';
    
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$nameArhive");
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary"); 
    readfile($nameArhive);
    unlink($nameArhive);
} else {
    die ('Произошла ошибка при создании архива');
}

Pamagity

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2018-04-15
@BorisKorobkov

Delete
F-I: Archiving F-I:
tar -cf ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question