温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP便利文件夹下所有文件,创建压缩包

发布时间:2020-09-06 13:16:27 来源:网络 阅读:1139 作者:phphan 栏目:web开发

把所有文件保存在$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. }   


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI