当前位置:网站首页>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
边栏推荐
- 拷贝构造函数
- 代理服务器
- Explanation of the second I interval of 2020 Niuke summer multi school training camp
- Rust:如何 match 匹配 String 字符串?
- 对象的动态建立和释放,赋值和复制
- GDB debugger installation and use
- 解决ArcGIS分区统计显示太多唯一值执行失败
- How SYSTEMd uses / etc / init D script
- Swagger2 generates API documents
- The onnx model of yolov5 removes the transfer layer
猜你喜欢

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

PHP junior programmers, take orders and earn extra money

Cross domain issues - allow origin header contains multiple values but only one is allowed

A solution to replace not in in SQL

类和对象的初始化(构造函数与析构函数)

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

gcc ,g++,gdb的安装

Rust的闭包类型(Fn, FnMut, FnOne的区别)

How SYSTEMd uses / etc / init D script

Export the articles written in CSDN to PDF format
随机推荐
Graduation project, curriculum link, student achievement evaluation system
Object转Json差异之Gson fastJson jackson 修改字段名
pyppeteer爬虫
【UDS统一诊断服务】四、诊断典型服务(4)— 在线编程功能单元(0x34-0x38)
xlsxwriter.exceptions.FileCreateError: [Errno 13] Permission denied问题
Record the installation and configuration of gestermer on TX2, and then use GST RTSP server
Initialization of classes and objects (constructors and destructors)
破解滑动验证码
【OpenCV】使用 FileStorage 读写 Eigen 向量
多线程爬取马可波罗网供应商数据
四元数乘法
爬西瓜视频url
selenium+PhantomJS破解滑动验证2
Rust:在线程池中共享变量
Friend function, friend class, class template
Cross domain issues - allow origin header contains multiple values but only one is allowed
Robocode教程5——Enemy类
相机标定:关键点法 vs 直接法
【UDS统一诊断服务】四、诊断典型服务(5)— 功能/元件测试功能单元(例行程序功能单元0x31)
使用TransmittableThreadLocal实现参数跨线程传递