当前位置:网站首页>TP下载文件夹,压缩文件夹并下载
TP下载文件夹,压缩文件夹并下载
2022-04-21 06:16:00 【迷客】
TP下载文件夹,压缩文件夹并下载
一个文件夹是无法直接下载的,需要先把要下载的文件夹压缩,然后再将已经压缩好的文件下载到浏览器
/** * 封装压缩文件 * @param $name */
public function zipFile($name){
$zip = new \ZipArchive();
// 需要压缩的文件路劲
$file_name = ROOT_PATH.'public'.DS.'uploads'.DS.$name;
//压缩文件名
$filename = 'uploads/download';
if ($zip->open($filename, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) === TRUE) {
if (file_exists($file_name)) {
Zip::addFileToZip($file_name, $zip);//执行递归压缩目录的操作
} else {
throw new NotFoundHttpException('该目录不存在或被删除');
}
}
//打包zip
$zip->close();
}
封装zip类,也可以用作方法
class Zip extends Controller
{
public static function addFileToZip($path, &$zip,$root='')
{
$handler = opendir($path); //打开当前文件夹由$path指定。
!$root && $root = $path;
while (($filename = readdir($handler)) !== false)
{
if ($filename != "." && $filename != "..")
{
//文件夹文件名字为'.'和‘..’,不要对他们进行操作
if (is_dir($path . "/" . $filename))
{
// 如果读取的某个对象是文件夹,则递归
self::addFileToZip($path . "/" . $filename, $zip, $root);
}
else
{
//将文件加入zip对象
$pathFilename=$path . "/" . $filename;
$zip->addFile($pathFilename, str_replace($root.'/','',$pathFilename));
}
}
}
@closedir($path);
}
}
/** * 封装下载excel */
public function head_upload($file_name)
{
$file_name = iconv("utf-8", "gbk//IGNORE", $file_name);; //中文表名转码
$file_dir = $_SERVER['DOCUMENT_ROOT']."/excel_template/"; //下载文件存放目录
//检查文件是否存在
if (!file_exists($file_dir . $file_name)) {
header('HTTP/1.1 404 NOT FOUND');
} else {
//以只读和二进制模式打开文件
$file = fopen($file_dir . $file_name, "rb");
//告诉浏览器这是一个文件流格式的文件
Header("Content-type: application/octet-stream");
//请求范围的度量单位
Header("Accept-Ranges: bytes");
//Content-Length是指定包含于请求或响应中数据的字节长度
Header("Accept-Length: " . filesize($file_dir . $file_name));
//用来告诉浏览器,文件是可以当做附件被下载,下载后的文件名称为$file_name该变量的值。
Header("Content-Disposition: attachment; filename=" . $file_name);
//读取文件内容并直接输出到浏览器
echo fread($file, filesize($file_dir . $file_name));
fclose($file);
exit ();
}
}
注意:文件夹的路劲,根据个人项目来定,下载文件的方法tp6也可以,压缩文件的方法,tp6没试过,调试一下应该都没问题。
版权声明
本文为[迷客]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43106047/article/details/119804159
边栏推荐
猜你喜欢

How does excel form become latex format?

ESP32 LVGL8.1 M5Stack + LVGL + IDF 详细的移植教程 (27)

【Thread X】NetX Duo PTP校时客户端

ESP32 LVGL8.1 ——img 图片 (img 20)

"Fundamentals of digital electronic technology" 4.3 learning notes of some commonly used combinational logic circuits

Canvas drawing Luffy

ESP32驱动编码器--SIQ-02FVS3 (Vscode + IDF)

Implementation of prototype networks based on pytorch

【KSZ8863】KSZ8863交换机芯片的信息汇总与打板验证结果

GD单片机Keil环境搭建
随机推荐
const关键字、变量、函数用const修饰
ESP32 LVGL8.1 ——img 图片 (img 20)
国产GD芯片CAN过滤器
ESP32 (UART 485通讯)-串口之485通讯(3)
jvm 的 解释执行与 编译执行
vee-validate 验证
每日CISSP认证常错题(2022年4月12日)
每日网安认证测试题(2022年4月13日)
Qt TableWidget插入QComboBox下拉框
【KSZ8863】KSZ8863交换机芯片的信息汇总与打板验证结果
ESP32 LVGL8.1 ——Calendar 日历 (Calendar 25)
Implementation of prototype networks based on pytorch
Flat (flat lattice transformer) code implementation
curl命令
Simulation system of DC motor drive control based on STM32F103 chip
多文件上传(表单提交)
3. Date command problem in bat
Launcher3 secondary development modification removes drawers, displays icons in workspace, deletes Google search box and other operations.
【SSM整合】1. 基本环境搭建
3.bat中date命令问题