|
把所有文件保存在$file_array的数组中,然后对其进行操作,然后压缩文件zip,进行下载。
$dir="文件路径"; //这里输入其它路径
//PHP遍历文件夹下所有文件
$handle=opendir($dir.".");
// echo "文件:";
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..") {
//输出文件名
$file_url.=$file."/";
}
}
$file_array=explode("/", $file_url);
closedir($handle);
//创建zip的压缩包
$zip = new ZipArchive();
if ($zip->open(G_FLEXPAPER_TMP_FILES.'/123456/text123.zip', ZipArchive::OVERWRITE) === TRUE)
{
$zip->addFile(G_FLEXPAPER_TMP_FILES.'123456/a.doc');//假设加入的文件名是image.txt,在当前路径下
if(isset($file_array)){
for ($i=0;$iaddFromString(G_FLEXPAPER_TMP_FILES.'/123456/'.$file_array[$i], 'file content goes here');
}
}
$zip->close();
}
//下载 注意在JS里输出url,window.open(url);
- $file_name = "xxx.rar"; //下载文件名
- $file_dir = "./up/"; //下载文件存放目录
- //检查文件是否存在
- if (! file_exists ( $file_dir . $file_name )) {
- echo "文件找不到";
- exit ();
- } else {
- //打开文件
- $file = fopen ( $file_dir . $file_name, "r" );
- //输入文件标签
- Header ( "Content-type: application/octet-stream" );
- Header ( "Accept-Ranges: bytes" );
- Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );
- Header ( "Content-Disposition: attachment; filename=" . $file_name );
- //输出文件内容
- //读取文件内容并直接输出到浏览器
- echo fread ( $file, filesize ( $file_dir . $file_name ) );
- fclose ( $file );
- exit ();
- }
|
|
|