PHP获取远程线上图片 压缩包格式让用户下载
2021/5/22 22:25:12
本文主要是介绍PHP获取远程线上图片 压缩包格式让用户下载,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
/** * 下载图片并生成压缩包 * @param $data 图片数组,一维 * @param $dir 存放路径 * @return string */ public function demoOne() { //线上图片链接 $url = ["https://z3.ax1x.com/2021/05/22/gLICwR.jpg","https://z3.ax1x.com/2021/05/22/gLTRF1.jpg"]; //保存的位置 $dir = "img/"; //判断目录是否存在 不存在则创建 if (!file_exists($dir)) { mkdir($dir, 0777, true); } // 压缩包文件名 $zipName = date('His') . mt_rand(1000, 9999) . '.zip'; // 文件路径 $zipNameUrl = $dir . $zipName; $zip = new \ZipArchive(); if ($zip->open($zipNameUrl, \ZipArchive::OVERWRITE) !== true) { //OVERWRITE 参数会覆写压缩包的文件 文件必须已经存在 if ($zip->open($zipNameUrl, \ZipArchive::CREATE) !== true) { // 文件不存在则生成一个新的文件 用CREATE打开文件会追加内容至zip return '下载失败,文件夹不存在'; } } foreach ($url as $file) { //抓取图片内容 file_get_contents可用curl代替 curl效率高 $fileContent = file_get_contents($file); //添加图片 $zip->addFromString(basename($file), $fileContent); } $zip->close(); //没有文件 if (!file_exists($zipNameUrl)) { return '下载失败,图片不存在或无法下载'; } header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename=' . $zipName); //文件名 header("Content-Type: application/zip"); //zip格式的 header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件 header('Content-Length: ' . filesize($zipNameUrl)); //告诉浏览器,文件大小 flush(); @readfile($zipNameUrl); unlink($zipNameUrl); // 删除压缩包 不占空间 }
这篇关于PHP获取远程线上图片 压缩包格式让用户下载的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-29env: php: no such file or directory
- 2024-03-01php foreach break
- 2024-02-26Exception参数 php-icode9专业技术文章分享
- 2023-12-30PHP文件批量上传-icode9专业技术文章分享
- 2023-12-30thinkphp6 withJoin-icode9专业技术文章分享
- 2023-12-27MagicArray:像php一样,让Go业务代码不再卷!
- 2023-11-18centos7编译安装PHP教程。
- 2023-11-18centos7编译安装phpMyAdmin教程。
- 2023-10-13唱衰这么多年,PHP 仍然还是你大爷!
- 2023-07-25PHP8,性能更好,语法更好,类型安全更完善