1 Star 0 Fork 0

hw18708118867/fileDownload

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
download_func.php 1.68 KB
Copy Edit Raw Blame History
hw18708118867 authored 2016-12-15 23:08 +08:00 . sample function for file download
<?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\"");//告诉浏览器返回的文件的名称。
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);
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/miraclehw/fileDownload.git
git@gitee.com:miraclehw/fileDownload.git
miraclehw
fileDownload
fileDownload
master

Search