当前位置:网站首页>TP download folder, compress folder and download
TP download folder, compress folder and download
2022-04-23 06:34:00 【Fan Ke】
TP Download folder , Compress the folder and download
A folder cannot be downloaded directly , You need to compress the folder you want to download first , Then download the compressed file to the browser
/** * Encapsulate compressed files * @param $name */
public function zipFile($name){
$zip = new \ZipArchive();
// The file needs to be compressed
$file_name = ROOT_PATH.'public'.DS.'uploads'.DS.$name;
// Compressed file name
$filename = 'uploads/download';
if ($zip->open($filename, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) === TRUE) {
if (file_exists($file_name)) {
Zip::addFileToZip($file_name, $zip);// Perform the operation of recursively compressing the directory
} else {
throw new NotFoundHttpException(' The directory does not exist or has been deleted ');
}
}
// pack zip
$zip->close();
}
encapsulation zip class , It can also be used as a method
class Zip extends Controller
{
public static function addFileToZip($path, &$zip,$root='')
{
$handler = opendir($path); // Open the current folder by $path Appoint .
!$root && $root = $path;
while (($filename = readdir($handler)) !== false)
{
if ($filename != "." && $filename != "..")
{
// The folder file name is '.' and ‘..’, Don't operate on them
if (is_dir($path . "/" . $filename))
{
// If an object read is a folder , Then recursion
self::addFileToZip($path . "/" . $filename, $zip, $root);
}
else
{
// Add files to zip object
$pathFilename=$path . "/" . $filename;
$zip->addFile($pathFilename, str_replace($root.'/','',$pathFilename));
}
}
}
@closedir($path);
}
}
/** * Package download excel */
public function head_upload($file_name)
{
$file_name = iconv("utf-8", "gbk//IGNORE", $file_name);; // Chinese table name transcoding
$file_dir = $_SERVER['DOCUMENT_ROOT']."/excel_template/"; // Download file storage directory
// Check if the file exists
if (!file_exists($file_dir . $file_name)) {
header('HTTP/1.1 404 NOT FOUND');
} else {
// Open the file in read-only and binary mode
$file = fopen($file_dir . $file_name, "rb");
// Tell the browser that this is a file stream format
Header("Content-type: application/octet-stream");
// The unit of measure of the requested scope
Header("Accept-Ranges: bytes");
//Content-Length Is to specify the byte length of the data contained in the request or response
Header("Accept-Length: " . filesize($file_dir . $file_name));
// Used to tell the browser , Files can be downloaded as attachments , The name of the downloaded file is $file_name The value of the variable .
Header("Content-Disposition: attachment; filename=" . $file_name);
// Read the contents of the file and output it directly to the browser
echo fread($file, filesize($file_dir . $file_name));
fclose($file);
exit ();
}
}
Be careful : The road strength of the folder , According to individual projects , How to download files tp6 It's fine too , How to compress files ,tp6 Never tried. , Debugging should be all right .
版权声明
本文为[Fan Ke]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210614037873.html
边栏推荐
猜你喜欢

Detailed arrangement of knowledge points of University probability theory and mathematical statistics

Vscode custom comments

安装pyshp库

Completely clean up MySQL win

【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)

Robocode教程8——AdvancedRobot

ArcGIS表转EXCEL超出上限转换失败

C#【文件操作篇】PDF文件和图片互相转换

Dynamic creation and release, assignment and replication of objects

P1586 solution to tetragonal theorem
随机推荐
基于Sentinel+Nacos 对Feign Client 动态添加默认熔断规则
Feign请求日志统一打印
Rust的闭包类型(Fn, FnMut, FnOne的区别)
Cross domain issues - allow origin header contains multiple values but only one is allowed
根据SQL语句查询出的结果集,将其封装为json
Completely clean up MySQL win
Protection of shared data
用C语言实现重写strcmp等四个函数
Robocode教程7——雷达锁定
Jeu de devinettes
【UDS统一诊断服务】四、诊断典型服务(3)— 读故障信息功能单元(存储数据传输功能单元)
clion安装教程
四元数乘法
Programmers can also write novels
Static member
类和对象的初始化(构造函数与析构函数)
C语言输入和输出(printf和scanf函数、putchar和getchar函数)
非参数化相机畸变模型简介
【UDS统一诊断服务】三、应用层协议(2)
word排版遇到的格式问题