PHP下载流(边打包边下载)

我用的是TP5的框架。

一、安装Zipstream依赖

composer require mcnetic/zipstreamer

二、在extend/org目录下创建一个类Steamer.php

<?php
namespace org;

use ZipStreamer\ZipStreamer;

class Streamer {

    // streamer instance
    private $streamerInstance;
    
    public function __construct() {
        
        $this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]);
        
    }
    
    /**
     * Send HTTP headers
     * @param string $name
     */
    public function sendHeaders($name) {
        $extension = $this->streamerInstance instanceof ZipStreamer ? '.zip' : '.tar';
        $fullName = $name . $extension;
        $this->streamerInstance->sendHeaders($fullName);
    }
    
    /**
     * Stream directory recursively
     * @param string $dir
     * @param string $internalDir
     */
    public function addDirRecursive($dir, $internalDir='') {
        $dirname = basename($dir);
        $rootDir = $internalDir . $dirname;
        if (!empty($rootDir)) {
            $this->streamerInstance->addEmptyDir($rootDir);
        }
        $internalDir .= $dirname . '/';
        // prevent absolute dirs
        $internalDir = ltrim($internalDir, '/');
        $handler = opendir($dir); //打开当前文件夹由$path指定。
        while (($filename = readdir($handler)) !== false) {
            
             if ($filename != "." && $filename != "..") {
                $file = $dir . '/' . $filename;
                if (is_file($file)) {
                    $filesize = filesize($file);
                    $fh = fopen($file, 'r');
                    $this->addFileFromStream($fh, $internalDir . $filename, $filesize);
                    fclose($fh);
                } elseif (is_dir($file)) {
                    $this->addDirRecursive($file, $internalDir);
                }
            }
        }

        
    }
    
    /**
     * Add a file to the archive at the specified location and file name.
     *
     * @param string $stream Stream to read data from
     * @param string $internalName Filepath and name to be used in the archive.
     * @param int $size Filesize
     * @return bool $success
     */
    public function addFileFromStream($stream, $internalName, $size) {
        if ($this->streamerInstance instanceof ZipStreamer) {
            return $this->streamerInstance->addFileFromStream($stream, $internalName);
        } else {
            return $this->streamerInstance->addFileFromStream($stream, $internalName, $size);
        }
    }

    /**
     * Add an empty directory entry to the archive.
     *
     * @param string $dirName Directory Path and name to be added to the archive.
     * @return bool $success
     */
    public function addEmptyDir($dirName) {
        return $this->streamerInstance->addEmptyDir($dirName);
    }

    /**
     * Close the archive.
     * A closed archive can no longer have new files added to it. After
     * closing, the file is completely written to the output stream.
     * @return bool $success
     */
    public function finalize() {
        return $this->streamerInstance->finalize();
    }
}

提醒下:他这个压缩格式是zip64的,我反正碰到了解压失败的问题,格式不对。所以我改了zipstreamer的源码,mcnetic\zipstreamer\src下的ZipStreamer.php,将true:

private $zip64 = True;

改成false,改成false的话,他的格式就是zip格式了。

三、在下载的逻辑里实例化类

$streamer = new \org\Streamer();

1、设置头部信息

$streamer->sendHeaders($title); //$title就是打包下载的文件名

2、设置清除缓冲区、脚本执行限制的时间及用户断开链接后继续执行

while (ob_get_level()) {
     ob_end_clean();
}
            
set_time_limit(0);
ignore_user_abort(true);

3、下载打包

//我这是foreach遍历文件中的一段代码,参考下
$file = $dir . '/' . $value['path'];
if (is_file($file)) {
    $fileSize = filesize($file); 
    $fh = fopen($file, 'r');
    $streamer->addFileFromStream($fh, basename($file), $fileSize);
     fclose($fh);
} elseif (is_dir($file)) {
     $streamer->addDirRecursive($file);
}

4、下载完成,即流结束

$streamer->finalize();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值