php实现文件下载函数

2025-11-06 0 824

php文件下载

<?php /** * [download_file 下载文件的函数(大文件)] * @param [type] $path_name [下载文件的路径] * @param [type] $save_name [保存文件的名字] * @return [type] [直接输出] */ function download_file($path_name, $save_name){ ob_end_clean();//清除缓冲区 $file = fopen($path_name, “rb”) or die(“Can not find file: $path_name\n”);//只读的方式将文件打开,b表示强制二进制形式 Header(“Content-type: application/octet-stream”);//通过这句代码客户端浏览器就能知道服务端返回的文件形式。 Header(“Content-Transfer-Encoding: binary”);//Content-Transfer-Encoding: binary Header(“Accept-Ranges: bytes”);//告诉客户端浏览器返回的文件大小是按照字节进行计算的 Header(“Content-Length: “.filesize($path_name));//告诉浏览器返回的文件大小 Header(“Content-Disposition: attachment; filename=$save_name);//告诉浏览器返回的文件的名称。filename=\”$save_name\”” while (!feof($file)) {//判断文件是否达到末尾 echo fread($file, 32768);//每次读取32768字节的长度并输出 } fclose($file);//关闭文件句柄 } /** * [download_file2 下载文件函数二(小文件)] * @param [type] $file_path [下载文件的路径] * @return [type] [直接输出] */ function download_file2($file_path){ if(!file_exists($file_path)){ die(file does not exists!); } $fileName = basename($file_path);//获取路径中的文件名 header(Content-Disposition:attachment;filename=.$fileName);//告诉浏览器返回的文件名 header(Content-Length:.filesize($file_path));//告诉浏览器返回文件的大小 readfile($file_path); }

以上就是php实现文件下载函数的详细内容,更多关于php实现文件下载函数的资料请关注码农网其它相关文章!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

遇见资源网 后端开发 php实现文件下载函数 https://www.ox520.com/4393.html

常见问题

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务