shemgp/http-send-file

向客户端发送文件,支持(多个)范围请求。它还能限制下载速度。

v2.0.4 2018-05-24 06:00 UTC

README

支持(多个)范围请求发送文件。它能够限制下载速度。它相当小巧简单。

这个类类似于PHP pecl中的php http_send_file

参考

https://php.ac.cn/manual/en/function.http-send-file.php

安装

使用composer,在"require"部分添加

composer require shemgp/http-send-file

使用示例

use diversen\sendfile;
$s = new sendfile();
        
// if you don't set type - we will try to guess it
$s->contentType('application/epub+zip');
        
// if you don't set disposition (file name user agent will see)
// we will make a file name from file
$s->contentDisposition('test.epub');
        
// chunks of 40960 bytes per 0.1 secs
// if you don't set this then the values below are the defaults
// approx 409600 bytes per sec
$s->throttle(0.1, 40960);

// file
$file = '/some/dir/test.epub';

// send the file
try {
    $s->send($file);
} catch (\Exception $e) {
    echo $e->getMessage();
}

因此,您可以这样做

use diversen\sendfile;
$s = new sendfile();

// file
$file = '/some/dir/test.epub';

// send the file
try {
    $s->send($file);
} catch (\Exception $e) {
    echo $e->getMessage();
}

// but check the headers if it is not
// working as expected as the guessing
// of content-type does not always work
// correctly. 

不发送content-disposition头部

// without sending content-disposition header
// 2. param = false
try {
    $s->send($file, false);
} catch (\Exception $e) {
    echo $e->getMessage();
}

发送文件作为内联

// Send as inline
// 3. param = false
try {
    $s->send($file, true, false);
} catch (\Exception $e) {
    echo $e->getMessage();
}

致谢

大部分代码是从这里获得的(并进行了重写)

http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/

过程在这里解释得很好

http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/

MIT © Dennis IversenShem Pasamba