完整代码:
public function downloadFile(){
$M = M($this->tableName);
$map['id'] = I('fileId');
$info = $M->where($map)->find();
$filepath = '.'.$info['path'];
if( !file_exists($filepath) ){
echo '文件不存在!';
exit;
}
//$M->where($map)->setInc('download');
$file = fopen($filepath,"r"); // 打开文件
// 输入文件标签
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".filesize($filepath));
Header("Content-Disposition: attachment; filename=" . $info['savename']);
// 输出文件内容
echo fread($file,filesize($filepath));
fclose($file);
exit;
}
以上代码为thinkphp下的方法
$this->tableName 为存储文件的数据表
根据fileId获取数据库响应文件
通过file_exists判断文件是否存在,如果不存在则输出错误信息
使用官方Model Context Protocol PHP SDK将PHP应用程序功能暴露为MCP工具、资源和提示,用于代理工作流。
通过fopen打开文件
在更具Header方法处理打开文件,以及通过fread()方法把文件输出。
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!










