php实现对文件的下载需要了解一定的http协议基础,php实现下载用到四条http协议的信息,用header进行会写处理
举一个案例:
复制代码 代码如下:
class Downfile {
function downserver($file_name){
$file_path = "./img/".$file_name;
//转码,文件名转为gb2312解决中文乱码
$file_name = iconv("utf-8","gb2312",$file_name);
$file_path = iconv("utf-8","gb2312",$file_path);
$fp = fopen($file_path,"r") or exit("文件不存在");
//定义变量空着每次下载的大小
$buffer = 1024;
//得到文件的大小
$file_size = filesize($file_path);
//header("Content-type:text/html;charset=gb2312");
//会写用到的四条http协议信息
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");//可以忽略
header("Content-Length: ".$file_size);//原文这里是Accept-Length经查阅http协议无此项
header("Content-Disposition:attachment;filename=".$file_name);
//字节技术器,纪录当前现在字节数
$count = 0;
while(!feof($fp) && $file_size-$count>0){
//从$fp打开的文件流中每次读取$buffer大小的数据
$file_data = fread($fp,$buffer);
$count+=$buffer;
//将读取到的数据读取出来
echo $file_data;
}
//关闭文件流
fclose($fp);
}
}
?>
调用这个函数传入文件名就能对文件实现下载,,不过要注意修改$file_path

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.