当前位置:网站首页>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
边栏推荐
猜你喜欢
[ThreadX] ThreadX source code reading plan (I)
Detailed arrangement of knowledge points of University probability theory and mathematical statistics
【UDS统一诊断服务】四、诊断典型服务(2)— 数据传输功能单元
For() loop parameter call order
The most practical chrome plug-in
拷贝构造函数
Gesture recognition research
Robocode教程5——Enemy类
【UDS统一诊断服务】三、应用层协议(1)
Rust的闭包类型(Fn, FnMut, FnOne的区别)
随机推荐
代理服务器
Robocode教程7——雷达锁定
Static member
进程间通信的方式
爬取手游网站游戏详情和评论(MQ+多线程)
解决ArcGIS分区统计显示太多唯一值执行失败
Friend function, friend class, class template
【UDS统一诊断服务】四、诊断典型服务(5)— 功能/元件测试功能单元(例行程序功能单元0x31)
【UDS统一诊断服务】四、诊断典型服务(4)— 在线编程功能单元(0x34-0x38)
如何安装jsonpath包
小区房价可视化
How SYSTEMd uses / etc / init D script
SVN简单操作命令
For() loop parameter call order
进程管理命令
The onnx model of yolov5 removes the transfer layer
对象的动态建立和释放,赋值和复制
PM2 deploy nuxt project
Matlab标定板角点检测原理
搭建jpress个人博客