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

Nginx配置之client_max_body_size和client_body_buffer_size

client_max_body_size client_max_body_size 默认 1M,表示 客户端请求服务器最大允许大小,在“Content-Length”请求头中指定。如果请求的正文数据大于client_max_body_size,HTTP协议会报错 413 Request Entity Too Large。就是说如果请求的正文大于client_max_body_size,一定是失败的。如果需要上传大文件,一定要修改该值。 client_body_buffer_size Nginx分配给请求数据的Buffer大小,如果请求的数据小于client_body_buffer_size直接将数据先在内存中存储。如果请求的值大于client_body_buffer_size小于client_max_body_size,就会将数据先存储到临时文件中,在哪个临时文件中呢? client_body_temp 指定的路径中,默认该路径值是/tmp/. 所以配置的client_body_temp地址,一定让执行的Nginx的用户组有读写权限。否则,当传输的数据大于client_body_buffer_size,写进临时文件失败会报错。 这个问题我们遇到过。

02
领券