Добавить обе папки и файлы в zip (PHP)

Я ищу, чтобы добавить как папки и файлы из соответствующего массива в ZIP-файл. Ниже мой код:

function listdir($start_dir='./assets') {
//Array that will contain the director
$files = array();

if (is_dir($start_dir)) {
$fh = opendir($start_dir);
while (($file = readdir($fh)) !== false) {
// Loop through the files, skipping '.' and '..', and recursing if necessary
if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
$filepath = $start_dir . '/' . $file;
if ( is_dir($filepath) )
$files = array_merge($files, listdir($filepath));
//else
array_push($files, $filepath);
}
closedir($fh);
} else {
// false if the function was called with an invalid non-directory argument
$files = false;
}
return $files;
}

//Array of all files
$allFiles = listdir('./assets');
print_r($allFiles);

//Gets all values with name "asset"$name = $_POST['asset'];

//If there are items in the array, zip them together
if($name!=0){
//Compares $_POST array with array of all files in directory
$result = array_intersect($allFiles, $name);

function zipFilesAndDownload($result){
$zip = new ZipArchive();
//create the file and throw the error if unsuccessful
if ($zip->open('SelectedAssets.zip', ZIPARCHIVE::CREATE )!==TRUE) {
exit("cannot open SelectedAssets.zip\n");
}
//add each files of $file_name array to archive
foreach($result as $allFiles)  {
$zip->addFile($allFiles);
}

$zip->close();
$zipped_size = filesize('SelectedAssets.zip');
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Type: application/force-download");// some browsers need this
header("Content-Disposition: attachment; filename=SelectedAssets.zip");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header("Content-Length:". " $zipped_size");
ob_clean();
flush();
readfile("SelectedAssets.zip");
unlink("SelectedAssets.zip"); // Now delete the temp file (some servers need this option)
exit;
}
if(isset($_POST['submit'])) {
//$file_names=$_POST['assets'];// Always sanitize your submitted data!!!!!!
//$file_names = filter_var_array($_POST['assets']);//works but it's the wrong method
$filter = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS) ;
$file_names = $filter['assets'] ;
//Archive name
$archive_file_name='DEMO-archive.zip';
//Download Files path
$file_path= getcwd(). './';
//call the function
zipFilesAndDownload($result);
} else {

print 'Something went wrong.';
exit;
}
} //Otherwise, don't.
else {
print_r("Please select a file.");
}

Приведенный выше код просматривает каталог и читает все папки и файлы. Затем, используя array_intersect, я сопоставил файлы, которые пользователь поместил в пустой массив, со всеми файлами в ранее найденном каталоге. Я тогда архивирую файлы, которые соответствуют и загружают.

У меня вопрос, как мне добавить папку в этот zip-файл? На данный момент, это только добавляет файлы и папки считаются пустыми.

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector