TrueAsync\SendFileOptions

(PHP 8.6+, true_async_server 0.4+)

HttpResponse::sendFile() 的配置 value-object。 不可变(final readonly class),通过构造函数的具名参数创建。

php
namespace TrueAsync;

final readonly class SendFileOptions
{
    public function __construct(
        public ?string             $contentType     = null,
        public SendFileDisposition $disposition     = SendFileDisposition::INLINE,
        public ?string             $downloadName    = null,
        public ?string             $cacheControl    = null,
        public bool                $etag            = true,
        public bool                $lastModified    = true,
        public bool                $acceptRanges    = true,
        public bool                $precompressed   = true,
        public bool                $conditional     = true,
        public bool                $deleteAfterSend = false,
        public ?int                $status          = null,
    ) {}
}

字段

字段类型默认作用
contentType?stringnull覆盖 MIME。null 时按扩展名自动判断。
dispositionSendFileDispositionINLINEINLINE —— 在浏览器中展示;ATTACHMENT —— 下载。影响 Content-Disposition
downloadName?stringnullContent-Disposition: attachment; filename=... 的文件名。配合 ATTACHMENT 使用。
cacheControl?stringnull直接放入 Cache-Controlnull 表示不发该头部。
etagbooltrue是否发送由 (mtime_ns, size, ino) 生成的 weak ETag
lastModifiedbooltrue是否发送 Last-Modified(IMF-fixdate)。
acceptRangesbooltrue是否支持 Range:(HTTP/1.1 partial content)。
precompressedbooltrueAccept-Encoding 兼容时是否查找 sidecar(*.br*.gz*.zst)。
conditionalbooltrueIf-Modified-Since / If-None-Match → 304。
deleteAfterSendboolfalse成功发送后 unlink($path)。适合 temp-file 的一次性下载。
status?intnull覆盖 HTTP 状态。例如默认本应 304 时,强制返回 200。

SendFileDisposition

php
namespace TrueAsync;

enum SendFileDisposition: string
{
    case INLINE     = 'inline';
    case ATTACHMENT = 'attachment';
}

示例

Inline PDF

php
use TrueAsync\SendFileOptions;

$res->sendFile('/var/storage/q1-report.pdf', new SendFileOptions(
    contentType:  'application/pdf',
    cacheControl: 'private, max-age=300',
));

带用户文件名的下载

php
use TrueAsync\SendFileOptions;
use TrueAsync\SendFileDisposition;

$res->sendFile('/var/storage/abc123.bin', new SendFileOptions(
    disposition:  SendFileDisposition::ATTACHMENT,
    downloadName: 'Q1 Report 2026.pdf',
    contentType:  'application/pdf',
));

一次性 temp-file

php
$tmp = '/tmp/export-' . bin2hex(random_bytes(8)) . '.csv';
generateExport($tmp);

$res->sendFile($tmp, new SendFileOptions(
    disposition:     SendFileDisposition::ATTACHMENT,
    downloadName:    'export.csv',
    contentType:     'text/csv; charset=utf-8',
    deleteAfterSend: true,
));

关闭 conditional GET(始终 200)

php
$res->sendFile('/var/storage/live.mp4', new SendFileOptions(
    conditional:   false,
    acceptRanges:  true,
    cacheControl:  'no-store',
));

不使用预压缩 sidecar(让引擎现编码)

php
$res->sendFile('/var/storage/big.json', new SendFileOptions(
    precompressed: false,
));

也可参考