- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Laravel 中创建 Zip 压缩文件并提供下载的实现方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
如果您需要您的用户支持多文件下载的话,最好的办法是创建一个压缩包并提供下载。下面通过本文给大家看下在 laravel 中的实现.
事实上,这不是关于 laravel 的,而是和 php 的关联更多,我们准备使用从 php 5.2 以来就存在的 ziparchive 类 ,如果要使用,需要确保php.ini 中的 ext-zip 扩展开启.
任务 1: 存储用户的发票文件到 storage/invoices/aaa001.pdf 。
下面是代码展示:
1
2
3
4
5
6
7
8
9
10
11
|
$zip_file
=
'invoices.zip'
;
// 要下载的压缩包的名称
// 初始化 php 类
$zip
=
new
\ziparchive();
$zip
->open(
$zip_file
, \ziparchive::create | \ziparchive::overwrite);
$invoice_file
=
'invoices/aaa001.pdf'
;
// 添加文件:第二个参数是待压缩文件在压缩包中的路径
// 所以,它将在 zip 中创建另一个名为 "storage/" 的路径,并把文件放入目录。
$zip
->addfile(storage_path(
$invoice_file
),
$invoice_file
);
$zip
->close();
// 我们将会在文件下载后立刻把文件返回原样
return
response()->download(
$zip_file
);
|
例子很简单,对吗?
* 。
任务 2: 压缩 全部 文件到 storage/invoices 目录中 。
laravel 方面不需要有任何改变,我们只需要添加一些简单的 php 代码来迭代这些文件.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
$zip_file
=
'invoices.zip'
;
$zip
=
new
\ziparchive();
$zip
->open(
$zip_file
, \ziparchive::create | \ziparchive::overwrite);
$path
= storage_path(
'invoices'
);
$files
=
new
\recursiveiteratoriterator(
new
\recursivedirectoryiterator(
$path
));
foreach
(
$files
as
$name
=>
$file
)
{
// 我们要跳过所有子目录
if
(!
$file
->isdir()) {
$filepath
=
$file
->getrealpath();
// 用 substr/strlen 获取文件扩展名
$relativepath
=
'invoices/'
.
substr
(
$filepath
,
strlen
(
$path
) + 1);
$zip
->addfile(
$filepath
,
$relativepath
);
}
}
$zip
->close();
return
response()->download(
$zip_file
);
|
到这里基本就算完成了。你看,你不需要任何 laravel 的扩展包来实现这个压缩方式.
ps:下面看下laravel从入门到精通之 文件处理 压缩/解压zip 。
1:将此软件包添加到所需软件包列表中composer.json 。
"chumper/zipper": "1.0.x" 。
2:命令行执行 。
composer update 。
3:配置app/config/app.php 。
1
2
|
add to providers chumper\zipper\zipperserviceprovider::
class
add to aliases
'zipper'
=> chumper\zipper\zipper::
class
|
4:遍历文件打包至压缩包 。
1
2
3
4
5
6
7
8
|
$files
=
array
();
foreach
(
$student
as
$key
=>
$data
) {
if
(
$data
->photopath != null) {
$check
=
glob
(storage_path(
'photo/'
.
$data
->photopath));
$files
=
array_merge
(
$files
,
$check
);
}
}
zipper::make(storage_path() .
'/systemimg/'
.
$name
)->add(
$files
)->close();
|
5:读取压缩包文件 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
zipper::make( storage_path() .
'/photo/photos'
)->extractto(storage_path(
'temp'
));
$zip
=
new
\ziparchive();
//方法2:流处理,新建一个ziparchive的对象
$logfiles
= zipper::make(
$path
)->listfiles(
'/\.png$/i'
);
if
(
$zip
->open(
$path
) === true) {
foreach
(
$logfiles
as
$key
) {
$stream
=
$zip
->getstream(
$key
);
$str
= stream_get_contents(
$stream
);
//这里注意获取到的文本编码
$name
= iconv(
"utf-8"
,
"gb2312//ignore"
,
$key
);
file_put_contents
(storage_path() .
'\temp\\'
.
$name
,
$str
);
}
}
else
{
return
'{"statuscode":"300", "message":"上传失败,请检查照片"}'
;
}
|
总结 。
以上所述是小编给大家介绍的laravel 中创建 zip 压缩文件并提供下载的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:https://segmentfault.com/a/1190000018734385 。
最后此篇关于Laravel 中创建 Zip 压缩文件并提供下载的实现方法的文章就讲到这里了,如果你想了解更多关于Laravel 中创建 Zip 压缩文件并提供下载的实现方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我开发了一个具有基本安全性的 Spring Boot 应用程序。我有两个具有相同路径和不同 http 方法的端点。当我使用默认密码/使用 application.yml 中给出的密码包含基本安全性时,
我的代码是这样的: 或者,像这样: 如果我首先列出 webm 源,Firefox 4 会播放它,但 Firefox 3.6 也会尝试播放它(但会失败,因为它不支持 webm)。
我希望提供一个泛型类型作为类型参数而不首先将其解析为具体类型。换句话说,我正在寻找一种方法来指定从基类继承时可以使用的类型映射函数。 示例(不正确的)语法,希望比我能解释得更好: abstract c
我在 .NET 中编写了一些桌面应用程序,它们既提供了用于正常使用的前端 GUI,也提供了用于其他需求(例如扩展、调度、自动化、高级使用等)的命令行界面。命名两个可执行文件的最佳做法是什么,因为它们构
我最近在这里思考了很多关于屏幕抓取以及它可能是一项什么样的任务。所以我提出以下问题。 作为网站开发人员,您是否会公开简单的 API 以防止用户抓取屏幕,例如 JSON 结果? 然后这些结果可以实现缓存
我正在为一个项目使用 Dojo 1.9,但我不明白 dojo.provide 的正确替代方案与传统风格相比,AMD 风格。我正在阅读 this文档页面。 很明显,这就是旧语法映射到新语法的方式: 旧
我正在开发一个 Angular 应用程序。当我使用 ng serve 正常运行它时,它运行没有任何错误.但是,当我运行 ng build --prod ,它给出了以下错误。 ERROR in Ille
我有一个 Mac 应用程序。在我的 Mac 应用程序中,我的屏幕之一有一个包含文本字段的 scrollView。在同一屏幕上,我有一个需要提供打印选项的按钮。可以打印文本字段的文本。打印按钮应调用 M
我已经成功地为普通媒体文件提供媒体文件,但是当我尝试提供管理媒体文件时,我失败了。请帮我找出问题所在,因为我已经尝试解决问题几个小时但没有运气(也一直在谷歌搜索并阅读有关提供静态文件的 django
我正在尝试创建一个简单的错误处理项目,它会在收到错误(例如 404、422 或 500)后为 JSON 提供错误数据。我使用来自 this 的代码网站,但它不适合我。 我实际上有这两个类: 基本 Co
假设我有一个名为 Number 的类(class),我打算对 Number 进行大量相等比较对象。我担心通用 Number::equals(Object o) 的“开销”(类比较等...)方法。在这种
假定以下情况: 对等方A只希望将音频流发送给对等方B 对等B只希望将视频流发送给对等A 从而, 与创建报价 var sdpConstraints = { “必填”:{ 'OfferToReceiveA
因为我有一些角度,所以我想检查角度模数 360°: double angle = 0; double expectedAngle = 360; angle.Should().B
这是我的程序中构建的 monad 堆栈: type Px a = ReaderT PConf (State PState) a 其中 PConf 和 PState 是保存应用程序的配置和状态的任意数据
因为我有一些角度,所以我想检查角度模数 360°: double angle = 0; double expectedAngle = 360; angle.Should().B
我有一个小程序需要以某些权限运行,这意味着加载时会显示一条警告消息。如果用户拒绝警告消息,我想重定向到错误页面并解释发生了什么。有什么办法可以做到这一点吗? 我研究过让计时器运行并在特定时间段后重定向
从我可以从 Firebase 文档中推断出,似乎需要服务器来提供静态内容(html和 javascript),所以你需要有一台托管机器和一个静态内容服务器在某处启动并运行,或某些服务托管静态站点。 对
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 3 年前。 Improv
我的项目根目录的静态文件夹中有一个文本文件。 我想提供它,所以我创建了: @csrf_exempt def display_text(request): content = retur
我目前正在研究指针,为了进一步理解我正在尝试使用指针将两个数值数组连接成一个。代码如下所示。 #include void concat(int **pa,int **pb,int **pc) {
我是一名优秀的程序员,十分优秀!