輕松學(xué)習(xí)jQuery插件EasyUI EasyUI創(chuàng)建樹形菜單
894
2024-03-19
本文實(shí)例講述了PHP實(shí)現(xiàn)打包下載文件的方法。分享給大家供大家參考,具體如下:
/** * 下載文件 * @param $img * @return string */ public function Download($img) { $items = []; $names = []; if($img) { //用于前端跳轉(zhuǎn)zip鏈接拼接 $path_redirect = '/zip/'.date('Ymd'); //臨時(shí)文件存儲(chǔ)地址 $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(); $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跳轉(zhuǎn)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"); //告訴瀏覽器,這是二進(jìn)制文件 header('Content-Length: ' . filesize($zipname)); //告訴瀏覽器,文件大小 @readfile($zipname); } //刪除臨時(shí)文件 @unlink($zipname); } } return ''; } } /** * curl獲取鏈接內(nèi)容 * @param $url * @return mixed|string */ public function CurlDownload($url) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $errno = curl_errno($ch); $error = curl_error($ch); $res=curl_exec($ch); curl_close($ch); if($errno>0){ return ''; } return $res; } /** * 保存臨時(shí)文件 * @param $url * @param $dir * @param $content * @return array */ public function SaveFile( $url ,$dir , $content) { $fname = basename($url); //返回路徑中的文件名部分 $str_name = pathinfo($fname); //以數(shù)組的形式返回文件路徑的信息 $extname = strtolower($str_name['extension']); //把擴(kuò)展名轉(zhuǎn)換成小寫 $path = $dir.'/'.md5($url).$extname; $fp = fopen( $path ,'w+' ); fwrite( $fp , $content ); fclose($fp); return array( $path , $extname) ; }
引用:
$img = [['url'=>'地址url/1.jpg','name'=>'名字']]; Download($img);
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP操作zip文件及壓縮技巧總結(jié)》、《php文件操作總結(jié)》、《php正則表達(dá)式用法總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
#免責(zé)聲明#
本站[綠夏技術(shù)導(dǎo)航]提供的一切軟件、教程和內(nèi)容信息僅限用于學(xué)習(xí)和研究目的;不得將上述內(nèi)容用于商業(yè)或者非法用途,否則,一切后果請(qǐng)用戶自負(fù)。本站信息來自網(wǎng)絡(luò)收集整理,版權(quán)爭議與本站無關(guān)。您必須在下載后的24個(gè)小時(shí)之內(nèi),從您的電腦或手機(jī)中徹底刪除上述內(nèi)容。如果您喜歡該程序或內(nèi)容,請(qǐng)支持正版,購買注冊,得到更好的正版服務(wù)。我們非常重視版權(quán)問題,如有侵權(quán)請(qǐng)郵件[admin@lxwl520.com]與我們聯(lián)系進(jìn)行刪除處理。敬請(qǐng)諒解!