Итак, прежде всего, у меня есть эта папка структура.
Код: Выделить всё
uploads/
- student-records
-- John-Doe
--- files
---- Application
---- Transcripts
Итак, я получаю это в своем zip-файле laragon/mson/wp-content/uploads/student-records/John-Doe/files/...
Вот мой настоящий код
Код: Выделить всё
$student = $student_first_name .'-'. $student_last_name.'_'.$student_id;
$upload_dir = wp_upload_dir();
$source = $upload_dir['basedir'] ."/student-records/" . $student . "/files/";
$zipFile = $upload_dir['path'] ."/student-records/" . $student . "/".$student.".zip";
$zipFileRelativePath = dirname($zipFile)."/".$student.".zip" ;
// Initialize archive object
$zipArchive = new ZipArchive();
# Create a zip target
if( $zipArchive->open( $zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE ) !== TRUE ) {
exit("Unable to open file.");
}
// create recursive directory iterator
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sourceRelativePath, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST);
if (is_dir($sourceRelativePath)) {
foreach ($files as $file) {
$filePath = str_replace( '\\', '/', $file->getRealPath() );
if (is_dir($file)) {
$zipArchive->addEmptyDir(str_replace($source . '/', '', $filePath . '/'));
} else if (is_file($file)) {
$zipArchive->addFromString(str_replace($source . '/', '', $filePath), file_get_contents($filePath));
}
}
} else if (is_file($source)) {
$zipArchive->addFromString(basename($source), file_get_contents($source));
}
$zipArchive->close();
echo ' Download';
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... ectories-i
Мобильная версия