一、此方法能夠下載圖片、壓縮包、pdf(親測),應該全部類型的文件均可如下載到本地,能夠試一下web
//遠程路徑,名稱,文件後綴 function downImgRar($url,$rename,$ext){ switch ($ext) { case 'jpg': //下載圖片 $file_path = 'uploads/images/'; break; case 'png': //下載圖片 $file_path = 'uploads/images/'; break; case 'pdf': //下載PDF $file_path = 'uploads/pdf/'; break; case 'rar': //下載壓縮包 $file_path = 'uploads/rar/'; break; case 'zip': //下載壓縮包 $file_path = 'uploads/rar/'; break; default: $file_path = 'uploads/files/'; break; } $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); $rawdata=curl_exec ($ch); curl_close ($ch); // 使用中文文件名須要轉碼 $fp = fopen($file_path.iconv('UTF-8', 'GBK', $rename).".".$ext,'w'); fwrite($fp, $rawdata); fclose($fp); // 返回路徑 return $_SERVER['DOCUMENT_ROOT'].$file_path.$rename.".".$ext; }
二、下載壓縮文件,這個只能夠下載壓縮文件windows
// 下載壓縮包 function downRar($file_path) { $file_name = '/uploads/rar/2009323162920-維C銀翹片說明書.rar'; $file_name = iconv("utf-8","gbk//IGNORE",$file_name); // 特別注意!特別注意!特別注意這裏,windows下必須開轉碼,否則直接文件不存 $file_path = $_SERVER['DOCUMENT_ROOT'] . $file_name;// 好比windows下這裏個人是 "D:/web/public/uploads/rar/2009323162920-維C銀翹片說明書.rar" //判斷若是文件存在,則跳轉到下載路徑 if (!file_exists($file_path)) { die("文件不存在!"); } $fp = fopen($file_path, "r+") or die('打開文件錯誤'); //下載文件必需要將文件先打開。寫入內存 $file_size = filesize($file_path); //返回的文件流 Header("Content-type:application/octet-stream"); //按照字節格式返回 Header("Accept-Ranges:bytes"); //返回文件大小 Header("Accept-Length:" . $file_size); //彈出客戶端對話框,對應的文件名 Header("Content-Disposition:attachment;filename=" . substr($file_name, strrpos($file_name, '/') + 1)); //防止服務器瞬間壓力增大,分段讀取 $buffer = 1024; while (!feof($fp)) { $file_data = fread($fp, $buffer); echo $file_data; } fclose($fp); die("下載成功!"); }