首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往
您找到你想要的搜索结果了吗?
是的
没有找到

PHP实现打包下载文件的方法示例

public function Download($img) { $items = []; $names = []; if($img) { //用于前端跳转zip链接拼接 $path_redirect = '/zip/'.date('Ymd'); //临时文件存储地址 $path = '/tmp'.$path_redirect; if(!is_dir($path)) { mkdir($path, 0777,true); } foreach ($img as $key => $value) { $fileContent = ''; $fileContent = $this->CurlDownload($value['url']); if( $fileContent ) { $__tmp = $this->SaveFile( $value['url'] , $path , $fileContent ); $items[] = $__tmp[0]; $names[] = $value['name'].'_'.($key+1).'.'.$__tmp[1]; } } if( $items ) { $zip = new ZipArchive(); /【要记得博客地址www.isres.com】/ $filename = time().'download.zip'; $zipname = $path.'/'.$filename; if (!file_exists($zipname)) { $res = $zip->open($zipname, ZipArchive::CREATE | ZipArchive::OVERWRITE); if ($res) { foreach ($items as $k => $v) { $value = explode("/", $v); $end = end($value); $zip->addFile($v, $end); $zip->renameName($end, $names[$k]); } $zip->close(); } else { return ''; } //通过前端js跳转zip地址下载,让不使用php代码下载zip文件 //if (file_exists($zipname)) { //拼接附件地址 //$redirect = 域名.$path_redirect.'/'.$filename; //return $redirect; //header("Location:".$redirect); //} //直接写文件的方式下载到客户端 if (file_exists($zipname)) { header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename=附件.zip'); //文件名 header("Content-Type: application/zip"); //zip格式的 header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件 header('Content-Length: ' . filesize($zipname)); //告诉浏览器,文件大小 @readfile($zipname); } //删除临时文件 @unlink($zipname); } } return ''; } } /**

02

Blazor 中如何下载文件到浏览器

最近想给之前文章《下载中转加速器 VPSDownloader.NET(.NET Core 程序部署到 Linux 系统)》中提到的 VPS 文件中转下载服务添加一个前端页面,其实之前也想使用热门的前端框架 Vue 来做,也做了点工作了,但是毕竟不是前端开发,上手起来还是比较慢的,而且引入了 NodeJS 等技术栈,和后端的 ASP.NET Core WebApi 也不共存于一个项目,开发和维护起来不太方便。后来了解到了发展如火如荼的 Blazor 框架,这个是微软开发的 .NET 领域的前端框架,在某种程度上和以前的 WebForm 有点类似,不过以前那个是微软自己搞的,大家都不待见它,现在这个是符合 Web 领域新标准 WebAssembly,而且 UI 方面也可以使用现有的成熟库。总之,使用 Blazor,可以使用 C# 代码来代替(当然也是兼容的)JavaScript 代码,使 .NET 开发人员能有极致的全栈开发体验,颠覆以往那种 “师夷长技以制夷” 的前端开发方式,所以决定学习使用一下。另外,本文的 Blazor 项目使用 Blazor Server 模式,而不是 Blazor WebAssembly 模式。

01
领券