当使用PHP发送文件时,可恢复下载吗?我们使用PHP脚本进行文件下载,因为我们不想公开可下载文件的绝对路径:header("Content-Type: $ctype");header("Content-Length: " . filesize($file));
header("Content-Disposition: attachment; filename=\"$fileName\"");
readfile($file);不幸的是,我们注意到通过这个脚本进行的下载不能被最终用户恢复。有任何方法来支持这种基于PHP的解决方案的可恢复下载吗?
3 回答
拉丁的传说
TA贡献1789条经验 获得超8个赞
Accept-Ranges: bytesRange: bytes=x-yxyxy - xHTTP/1.0 206 Partial Content.
$filesize = filesize($file);$offset = 0;$length = $filesize;if ( isset($_SERVER['HTTP_RANGE']) ) {
// if the HTTP_RANGE header is set we're dealing with partial content
$partialContent = true;
// find the requested range
// this might be too simplistic, apparently the client can request
// multiple ranges, which can become pretty complex, so ignore it for now
preg_match('/bytes=(\d+)-(\d+)?/', $_SERVER['HTTP_RANGE'], $matches);
$offset = intval($matches[1]);
$length = intval($matches[2]) - $offset;} else {
$partialContent = false;}$file = fopen($file, 'r');
// seek to the requested offset, this is 0 if it's not a partial content requestfseek($file, $offset);
$data = fread($file, $length);fclose($file);if ( $partialContent ) {
// output the right headers for partial content
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $offset . '-' . ($offset + $length) . '/' . $filesize);}
// output the regular HTTP headersheader('Content-Type: ' . $ctype);
header('Content-Length: ' . $filesize);header('Content-Disposition: attachment; filename="' .
$fileName . '"');header('Accept-Ranges: bytes');// don't forget to send the data tooprint($data);
白衣非少年
TA贡献1155条经验 获得超0个赞
Range
- 3 回答
- 0 关注
- 507 浏览
添加回答
举报
0/150
提交
取消
