当前位置:网站首页>Package and download files
Package and download files
2022-04-21 07:55:00 【Mars Murong】
function zip_file($packingFile, $zipDir, $zipFile = null)
{
if (!file_exists($zipDir)) {
mkdir($zipDir, 0777, true);
}
$zipFile = $zipFile ?: md5(date('ymdhis')) . '.zip';
$zip = new \ZipArchive();
fopen($zipDir . $zipFile, "w");
if ($zip->open($zipDir . $zipFile, \ZipArchive::OVERWRITE) === TRUE) {
if (is_array($packingFile)) {
foreach ($packingFile as $file) {
if (isset($file['path']) && !empty($file['path'])) {
$path = strpos($file['path'], 'http') !== false ? public_path(substr($file['path'], strpos($file['path'], '/', strpos($file['path'], '//') + 2))) : public_path($file['path']);
$zip->addFile($path, isset($file['packing_name']) && !empty($file['packing_name']) ? '/' . $file['packing_name'] . '.' . pathinfo($path, PATHINFO_EXTENSION) : null);
}
}
} else {
$zip->addFile($packingFile);
}
}
$zip->close(); // Turn off the processed zip file
}
public function download($data)
{
$packingFile = Drawing::where('id', $data['drawing_id'])->get([DB::raw($data['type'] . '_img as path'), DB::raw('code as packing_name')]);
if (!$packingFile->count()) {
return $this->formatReturn(null, 400, ' Picture file not found ');
}
$packingFile = $packingFile->toArray();
if (empty(array_filter(array_column($packingFile, 'path')))) {
return $this->formatReturn(null, 400, ' No pictures of the corresponding type found ');
}
$zipPath = '/zip/';
$zipName = date('ymdhis') . '- Drawing file .zip';
zip_file($packingFile, public_path($zipPath), $zipName);
if (!file_exists(public_path($zipPath . $zipName))) {
return $this->formatReturn(null, 400, ' Download failed ');
}
return $this->formatReturn(['url' => $zipPath . $zipName]);
}
版权声明
本文为[Mars Murong]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210629261258.html
边栏推荐
猜你喜欢

网关与分布式id

343. Find the product of decomposed integers and maximize integer break

STM32系统和自定义bootloader的实现和应用

343. 求分解整数的乘积最大化Integer Break

物联网操作系统Zephyr(入门篇)之1.0 Zephyr简介

C语言指针进阶(1.一阶与二阶指针)

服务器部署svn环境

MongoDB 实验——数据备份和恢复和数据库优化

“产业数字化”的技术背景与现实作用

Record the problems and solutions encountered in using fastjson message converter
随机推荐
Foundation of golang project development
Use of basic components of flutter
leetcode 206. Reverse linked list
Leetcode title -- 386 Dictionary rank, DFS
【牛客刷题18】 查找两个字符串a,b中的最长公共子串
343. 求分解整数的乘积最大化Integer Break
Panic and Recover
迅为i.MX6Q开发板Openwrt 文件系统构建
Server deployment SVN environment
Files and Directories
从零开始学习C语言日记——DAY27扫雷
leetcode 19. Delete the penultimate node of the linked list
Dynamic generation of three-level menu
[question 31] create two identical pets
MongoDB 实验——数据备份和恢复和数据库优化
Flutter first experience
leetcode 59. Spiral matrix II
Flutter初体验
Unity性能优化之UI
Acrobat Pro DC 教程::如何使用文本和图片文件创建 PDF?