diversen / http-send-file
向客户端发送文件,支持(多个)范围请求。它还可以限制下载速度。
v3.1.2
2024-03-03 14:37 UTC
README
支持(多个)范围请求的文件发送。它能够限制下载速度,并在请求中添加 etag。它相当小巧简单。
安装
使用 composer 添加到 "require" 部分
composer require diversen/http-send-file
使用示例
use Diversen\Sendfile; $s = new Sendfile(); // if you don't set type - we will try to guess it $s->setContentType('application/epub+zip'); // if you don't set disposition (file name user agent will see) // we will make a file name from file $s->setContentDisposition('test.epub'); // Expires header. Default is a date in the past $s->setExpires(3600); // chunks of 40960 bytes per 0.1 secs // if you don't set this then the values below are the defaults // approx 40960 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(); }
测试笔记
构建镜像
docker build -t php-apache2 .
运行 docker 镜像
docker run -d -p 8080:80 -v $(pwd):/var/www/html --name test-send-file php-apache2
启用自动加载
composer install
在浏览器中访问: http://localhost:8080/test
或者使用 curl,例如。
curl -v -L -O -C - http://localhost:8080/test/send_large_file.php
致谢
大部分代码是从这里获取(并重写的)
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 Iversen