当前位置:网站首页>Laravel packages multiple files and downloads them
Laravel packages multiple files and downloads them
2022-04-21 07:55:00 【Mars Murong】
// Clear all files in the path
delete_dir(public_path() . "/upload/data/");
$res = Evidence::whereIn("id", $this->data['evidences_id'])
->get();
foreach ($res as $v) {
$type = $v->file_md5;
$file_url = $v->upload_path;
if (!is_dir(public_path() . "/upload/data/")) mkdir(public_path() . "/upload/data/", 0777);
$dir = public_path() . "/upload/data/{$type}";
if (!is_dir($dir)) mkdir($dir, 0777);
// Download remote files to local folder most 3 Otherwise, the download fails
$n = 0;
do {
if (@copy(trim($file_url), public_path() . "/upload/data/{$type}/" . basename($file_url))) {
break;
} else {
$n++;
sleep(1);
if ($n >= 3) {
Download::where("id", $this->download->id)->update(["status" => 2, "error_msg" => " File download failed "]);
break;
}
}
} while (true);
}
//2. Package and download local folders
// initialization zip name
$zipName = date("YmdHis") . 'file.zip';
$zip_file = str_replace("\\", '/', public_path("download/$type")) . $zipName;
$zip = new \ZipArchive();
$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
// The folder will be compressed
$path = str_replace("\\", '/', public_path("upload/data/{$type}"));
if (count($res) > 1) {
$path = str_replace("\\", '/', public_path("upload/data/"));
}
// iterator
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($files as $file) {
// Skip all subdirectories
if (!$file->isDir()) {
// Get file path
$filePath = $file->getRealPath();
// Get the file name
$fileName = $file->getFilename();
// $zip->addFile($filePath, $fileName);
$fp = fopen($filePath, "rb", 0);
$arr = explode('\\', $filePath);
$dir = $arr[count($arr) - 2];
$images = fread($fp, filesize($file));
$zip->addFromString("$dir/" .$fileName, $images);
}
}
$zip->close();
$url = config('app.url') . "download/$type$zipName";
chmod($zip_file, 0777);
Download::where("id", $this->download->id)->update(["status" => 1, "path" => $url]);
// return response()->download($zip_file, $zipName)->deleteFileAfterSend(true);// Download the generated zip file
版权声明
本文为[Mars Murong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210629261217.html
边栏推荐
- Spawning Processes and Exec‘ing Processes
- Dynamic programming fixed-point breakthrough -- leetcode topic 64 Minimum path sum
- Hackmyvm integrated target | driftingblues-9 (end)
- Advanced C language pointer (1. First and second order pointers)
- Implementation and application of STM32 system and custom bootloader
- Postgre (PG) - SQL script record
- Acrobat Pro DC 教程::如何使用文本和图片文件创建 PDF?
- php 判断是不是同一个月
- 记录使用fastjson消息转换器遇到的问题和解决方法
- 服务器部署svn环境
猜你喜欢

Understanding of data warehouse ODS, DW and DM concepts in data governance platform

Self made webserver from scratch (XV) -- the log base part is completed, and the practical widget double-buffering optimizes the asynchronous write performance

蓝牙开源协议栈BTstack之1.0 BTstack简介

leetcode 59. Spiral matrix II

Gateway and distributed ID

动态规划定点突破 --leetcode题目64.最小路径和

Introduction to zephyr 1.0 of Internet of things operating system zephyr (Introduction)

Record the problems and solutions encountered in using fastjson message converter

云服务器使用frp将本机(win10/win11)内网上的apache映射到外网

2022-04-20:小团去参加军训,军训快要结束了, 长官想要把大家一排n个人分成m组,然后让每组分别去参加阅兵仪式, 只能选择相邻的人一组,不能随意改变队伍中人的位置, 阅兵仪式上会进行打分,其中
随机推荐
Introduction to zephyr 1.0 of Internet of things operating system zephyr (Introduction)
Time and Duration and Epoch
服务器部署svn环境
Spawning Processes and Exec‘ing Processes
Testing and Benchmarking
C# asp.net 调百度文字识别接口
IDEA 使用@Autowired注解报红的解决办法
Usage notes of Axure product prototype tool
Div Click to collapse
物联网操作系统Zephyr(入门篇)之1.0 Zephyr简介
Implementation and application of STM32 system and custom bootloader
PostgreSQL 15 即将支持SQL 标准中的 MERGE 语句
云服务器使用frp将本机(win10/win11)内网上的apache映射到外网
【牛客刷题18】 查找两个字符串a,b中的最长公共子串
PHP format numbers
PHP去除字符串开头或末尾逗号
Unity performance optimization UI
类中的泛型以及泛型类型
PHP Chinese to English initials
leetcode 209. Minimum length subarray