当前位置:网站首页>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
边栏推荐
猜你喜欢

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

【UDS统一诊断服务】一、诊断概述(1)— 诊断概述
[ThreadX] h743zi + lan8720 + ThreadX + netx duo transplantation

对象的动态建立和释放,赋值和复制

Vscode custom comments

拷贝构造函数

Explanation of the second I interval of 2020 Niuke summer multi school training camp

SQL sorts according to the specified content

【无标题】

for()循环参数调用顺序
随机推荐
猜数字游戏
Jeu de devinettes
【UDS统一诊断服务】四、诊断典型服务(2)— 数据传输功能单元
爬取小米有品app商品数据
scikit-learn sklearn 0.18 官方文档中文版
Easy to use data set and open source network comparison website
Solution to the trial of ycu Blue Bridge Cup programming competition in 2021
深拷贝和浅拷贝的区别
【UDS统一诊断服务】四、诊断典型服务(6)— 输入输出控制单元(0x2F)
gst-launch-1.0用法小记
实现一个计算m~n(m<n)之间所有整数的和的简单函数
进程管理命令
对象的动态建立和释放,赋值和复制
【UDS统一诊断服务】四、诊断典型服务(3)— 读故障信息功能单元(存储数据传输功能单元)
用二进制进行权限管理
【无标题】
对象数组与对象指针
Dynamic creation and release, assignment and replication of objects
Log4j2跨线程打印traceId
Wechat applet request encapsulation