分享编程~
 

php多文件批量打包压缩下载类库不依赖其他扩展

php多文件批量打包压缩下载类库不依赖其他扩展,支持远程文件。

天天特卖抢好货

php多文件批量打包压缩下载类库不依赖其他扩展。支持远程文件,路径参数支持远程路径,绝对路径,相对路径,支持压缩包以及文件重命名。

调用示例:

<?php
include 'ZipFile.php'; // 类库引入
// 需要打包的文件
$files = array(
  array('src' => 'http://test.com/filetest/getfile/1.txt'),
  array('src' => './filetest/getfile/1.txt', 'name' => 'g2'),
  array('src' => 'http://test.yj521.com/test/img/d1.jpg','name' => 'a'),
  array('src' => 'http://test.yj521.com/test/img/d2.png', 'name' => 'b'),
  array('src' => 'http://test.com/filetest/getfile/1.jpg', 'name' => 'c'),
  array('src' => 'http://test.com/filetest/getfile/1.png', 'name' => 'd'),
  array('src' => 'http://test.com/filetest/getfile/1.gif', 'name' => 'e'),
  array('src' => 'E:/www/test/filetest/getfile/1.bmp', 'name' => 'f'),
);
$folder = 'img'; // 压缩包名称
$zipFile = new ZipFile();
$zipFile->download($folder, $files, '1');
?>

ZipFile.php文件类库源码:

<?php
class ZipFile {
    var $datasec = array ();
    var $ctrl_dir = array ();
    var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
    var $old_offset = 0;

    function unix2_dostime($unixtime = 0){
        $timearray = ($unixtime == 0) ? getdate () : getdate($unixtime);
        if ($timearray ['year'] < 1980){
            $timearray ['year'] = 1980;
            $timearray ['mon'] = 1;
            $timearray ['mday'] = 1;
            $timearray ['hours'] = 0;
            $timearray ['minutes'] = 0;
            $timearray ['seconds'] = 0;
        }
        return (($timearray ['year'] - 1980) << 25) | ($timearray ['mon'] << 21) | ($timearray ['mday'] << 16) | ($timearray ['hours'] << 11) | ($timearray ['minutes'] << 5) | ($timearray ['seconds'] >> 1);
    }
    function add_file($data, $name, $time = 0){
        $name = str_replace('\\', '/', $name);

        $dtime = dechex($this->unix2_dostime($time));
        $hexdtime = '\x' . $dtime [6] . $dtime [7] . '\x' . $dtime [4] . $dtime [5] . '\x' . $dtime [2] . $dtime [3] . '\x' . $dtime [0] . $dtime [1];
        eval('$hexdtime = "' . $hexdtime . '";');

        $fr = "\x50\x4b\x03\x04";
        $fr .= "\x14\x00";
        $fr .= "\x00\x00";
        $fr .= "\x08\x00";
        $fr .= $hexdtime;

        $unc_len = strlen($data);
        $crc = crc32($data);
        $zdata = gzcompress($data);
        $zdata = substr(substr($zdata, 0, strlen($zdata)- 4), 2);
        $c_len = strlen($zdata);
        $fr .= pack('V', $crc);
        $fr .= pack('V', $c_len);
        $fr .= pack('V', $unc_len);
        $fr .= pack('v', strlen($name));
        $fr .= pack('v', 0);
        $fr .= $name;

        $fr .= $zdata;
        $fr .= pack('V', $crc);
        $fr .= pack('V', $c_len);
        $fr .= pack('V', $unc_len);

        $this->datasec [] = $fr;

        $cdrec = "\x50\x4b\x01\x02";
        $cdrec .= "\x00\x00";
        $cdrec .= "\x14\x00";
        $cdrec .= "\x00\x00";
        $cdrec .= "\x08\x00";
        $cdrec .= $hexdtime;
        $cdrec .= pack('V', $crc);
        $cdrec .= pack('V', $c_len);
        $cdrec .= pack('V', $unc_len);
        $cdrec .= pack('v', strlen($name));
        $cdrec .= pack('v', 0);
        $cdrec .= pack('v', 0);
        $cdrec .= pack('v', 0);
        $cdrec .= pack('v', 0);
        $cdrec .= pack('V', 32);

        $cdrec .= pack('V', $this->old_offset);
        $this->old_offset  = strlen($fr);

        $cdrec .= $name;

        $this->ctrl_dir[] = $cdrec;
    }
    function add_path($path, $l = 0){
        $d = @opendir($path);
        $l = $l > 0 ? $l : strlen($path)   1;
        while($v = @readdir($d)){
            if($v == '.' || $v == '..'){
                continue;
            }
            $v = $path . '/' . $v;
            if(is_dir($v)){
                $this->add_path($v, $l);
            } else {
                $this->add_file(file_get_contents($v), substr($v, $l));
            }
        }
    }
    function file(){
        $data = implode('', $this->datasec);
        $ctrldir = implode('', $this->ctrl_dir);
        return $data . $ctrldir . $this->eof_ctrl_dir . pack('v', sizeof($this->ctrl_dir)) . pack('v', sizeof($this->ctrl_dir)) . pack('V', strlen($ctrldir)) . pack('V', strlen($data)) . "\x00\x00";
    }

    function add_files($files){
        foreach($files as $file){
            if (is_file($file)){
                $data = implode("", file($file));
                $this->add_file($data, $file);
            }
        }
    }
    function output($file){
        $fp = fopen($file, "w");
        fwrite($fp, $this->file ());
        fclose($fp);
    }

    function download($folder, $files, $type = '1'){
        $dfile = tempnam('/tmp', 'tmp');
        $filename = $folder.'.zip'; //下载的默认文件名
        $i = 0;
        foreach($files as $v){
            $i   ;
            if(!isset($v['src']) || !$v['src']){
                echo '<script>alert("文件路径不能为空");window.history.back(-1); </script>';
            }
            $src =  $v['src'];
            //if(@getimagesize($src)){
                $name = isset($v['name']) ? $v['name'] : $i.'_';
                $name = $name.'.'.$this->getFileSuffix($src);
                if($type == '1'){
                    $this->add_file(file_get_contents($src), $name);
                }else if($type == '2'){
                    $this->add_path(file_get_contents($src), $name);
                }
            //}
        }
        $this->output($dfile);
        // 下载文件
        ob_clean();
        header('Pragma: public');
        header('Last-Modified:'.gmdate('D, d M Y H:i:s') . 'GMT');
        header('Cache-Control:no-store, no-cache, must-revalidate');
        header('Cache-Control:pre-check=0, post-check=0, max-age=0');
        header('Content-Transfer-Encoding:binary');
        header('Content-Encoding:none');
        header('Content-type:multipart/form-data');
        header('Content-Disposition:attachment; filename="'.$filename.'"'); //设置下载的默认文件名
        header('Content-length:'. filesize($dfile));
        $fp = fopen($dfile, 'r');
        while(connection_status() == 0 && $buf = @fread($fp, 8192)){
            echo $buf;
        }
        fclose($fp);
        @unlink($dfile);
        @flush();
        @ob_flush();
        exit();
    }
    function getFileSuffix($filename){
        return strrpos($filename, '.') ? substr($filename, strrpos($filename, '.') 1) : '';
    }
}

?>


上一篇:php多文件打包压缩下载简单示例下一篇:js库html页面生成图片并下载之html2canvas插件
赞(3) 踩(0)
您说多少就多少,您的支持是我最大的动力
赏金
微 信
赏金
支付宝
本文连接: https://www.yj521.com/article/114.html
版权声明: 本文为原创文章,版权归《越加网》所有,分享转载请注明出处!