Created
November 10, 2017 06:30
-
-
Save shangdev/686d3c35b95e88cb87c9e9ab7047a27d to your computer and use it in GitHub Desktop.
PHP遍历目录并下载文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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