首页
学习
活动
专区
工具
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

Python 基于Python实现的ssh兼sftp客户端(下)

#!/usr/bin/env/ python # -*- coding:utf-8 -*- __author__ = 'laifuyu' import os import subprocess class OtherTools: def __init__(self): self.filepath_list = [] # 批量创建目录 def mkdirs_once_many(self, path): path = os.path.normpath(path) # 去掉路径最右侧的 \\ 、/ path = path.replace('\\', '/') # 将所有的\\转为/,避免出现转义字符串 head, tail = os.path.split(path) new_dir_path = '' # 反转后的目录路径 root = '' #根目录 if not os.path.isdir(path) and os.path.isfile(path): # 如果path指向的是文件,则继续分解文件所在目录 head, tail = os.path.split(head) if tail == '': return while tail: new_dir_path = new_dir_path + tail + '/' head, tail = os.path.split(head) root = head else: new_dir_path = root + new_dir_path # 批量创建目录 new_dir_path = os.path.normpath(new_dir_path) head, tail = os.path.split(new_dir_path) temp = '' while tail: temp = temp + '/' + tail dir_path = root + temp if not os.path.isdir(dir_path): os.mkdir(dir_path) head, tail = os.path.split(head)

02
领券