把所有文件保存在$file_array的数组中,然后对其进行操作,然后压缩文件zip,进行下载。

$dir="文件路径"; //这里输入其它路径
//PHP遍历文件夹下所有文件
       $handle=opendir($dir.".");
//        echo "文件:<br>";
        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');//假设加入的文件名是p_w_picpath.txt,在当前路径下
            if(isset($file_array)){
                for ($i=0;$i<count($file_array);$i++){
                    $zip->addFromString(G_FLEXPAPER_TMP_FILES.'/123456/'.$file_array[$i], 'file content goes here');
                }
            }
            $zip->close();
        }


//下载  注意在JS里输出url,window.open(url);

  1. $file_name = "xxx.rar";     //下载文件名    

  2. $file_dir = "./up/";        //下载文件存放目录    

  3. //检查文件是否存在    

  4. if (! file_exists ( $file_dir . $file_name )) {    

  5.     echo "文件找不到";    

  6.     exit ();    

  7. else {    

  8.     //打开文件    

  9.     $file = fopen ( $file_dir . $file_name"r" );    

  10.     //输入文件标签     

  11.     Header ( "Content-type: application/octet-stream" );    

  12.     Header ( "Accept-Ranges: bytes" );    

  13.     Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );    

  14.     Header ( "Content-Disposition: p_w_upload; filename=" . $file_name );    

  15.     //输出文件内容     

  16.     //读取文件内容并直接输出到浏览器    

  17.     echo fread ( $filefilesize ( $file_dir . $file_name ) );    

  18.     fclose ( $file );    

  19.     exit ();    

  20. }