计算机学习

您现在的位置是:首页 > php > 正文

php

php下载文件

hhb2022-07-21php77
$params = array_merge($params = $this->_requestParams, $this->input-&
$params = array_merge($params = $this->_requestParams, $this->input->get());
if (empty($params['url'])) {
	throw new \Exception('url不能为空');
}
// 清空缓冲区并关闭输出缓冲
ob_end_clean();
$parseUrl = parse_url($params['url']);
$path = $parseUrl['path'];
$file_name = basename($path);
$filePath = FCPATH . $parseUrl['path'];

//r: 以只读方式打开,b: 强制使用二进制模式

$fileHandle = fopen($filePath, "rb");

if ($fileHandle === false) {
	throw new \Exception('文件打开失败');
}
Header("Content-type: application/octet-stream");
Header("Content-Transfer-Encoding: binary");
Header("Accept-Ranges: bytes");
Header("Content-Length: " . filesize($filePath));
Header("Content-Disposition: attachment; filename=\"$file_name\"");
while (!feof($fileHandle)) {
	//从文件指针 handle 读取最多 length 个字节
	echo fread($fileHandle, 32768);
}
fclose($fileHandle);

 ob_end_clean();

这个要加上,要不下载的图片可能无法显示。

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~