Skip to content

Instantly share code, notes, and snippets.

@shangdev
Created November 10, 2017 06:30
Show Gist options
  • Save shangdev/686d3c35b95e88cb87c9e9ab7047a27d to your computer and use it in GitHub Desktop.
Save shangdev/686d3c35b95e88cb87c9e9ab7047a27d to your computer and use it in GitHub Desktop.
PHP遍历目录并下载文件
$zip = new \ZipArchive();
if ( $zip->open( $zip_name, \ZipArchive::CREATE ) !== true ) {
wp_die( 'Can not open file, or file creation failed' );
}
foreach ( $files_path as $path ) {
if ( file_exists( $path ) ) {
$zip->addFile( $path, basename( $path ) );
}
}
$zip->close();
header( "Cache-Control: public" );
header( "Content-Description: File Transfer" );
header( "Content-Disposition: attachment; filename=" . $zip_name );
header( "Content-Type: application/zip" );
header( "Content-Transfer-Encoding: binary" );
header( "Content-Length:" . filesize( $zip_name ) );
readfile( $zip_name );
if ( file_exists( $zip_name ) ) {
unlink( $zip_name );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment