国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

PHP實現,遠程下載文件到本地

2019-11-02 14:42:11
字體:
來源:轉載
供稿:網友

   經常寫采集器發布接口需要使用到遠程附件的功能,所以自己寫了一個PHP遠程下載文件到本地的函數,一般情況下已經夠用了,如果服務器支持CURL函數,程序則會優先選擇CURL,有需要的小伙伴可以參考下。

  代碼很簡單就不多廢話了,直接奉上:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 <?php echo httpcopy("http://www.baidu.com/img/baidu_sylogo1.gif");   function httpcopy($url, $file="", $timeout=60) { $file = empty($file) ? pathinfo($url,PATHINFO_BASENAME) : $file; $dir = pathinfo($file,PATHINFO_DIRNAME); !is_dir($dir) && @mkdir($dir,0755,true); $url = str_replace(" ","%20",$url);   if(function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $temp = curl_exec($ch); if(@file_put_contents($file, $temp) && !curl_error($ch)) { return $file; } else { return false; } } else { $opts = array( "http"=>array( "method"=>"GET", "header"=>"", "timeout"=>$timeout) ); $context
高分經典電影[www.aikan.tv/xzhtml/12/]
= stream_context_create($opts); if(@copy($url, $file, $context)) { //$http_response_header return $file; } else { return false; } } } ?>

  再來個遠程下載文件到服務器

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <form method="post"> <input name="url" size="50" /> <input name="submit" type="submit" /> </form> < ?php // maximum execution time in seconds set_time_limit (24 * 60 * 60); if (!isset($_POST['submit'])) die(); // folder to save downloaded files to. must end with slash $destination_folder = 'temp/';   $url = $_POST['url']; $newfname = $destination_folder . basename($url); $file = fopen ($url, "rb"); if ($file) { $newf = fopen ($newfname, "wb"); if ($newf) while(!feof($file)) { fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 ); } } if ($file) { fclose($file); } if ($newf) { fclose($newf); } ?>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 雅江县| 盱眙县| 芦山县| 靖宇县| 胶南市| 多伦县| 阳曲县| 浏阳市| 阿尔山市| 若羌县| 江西省| 辽阳县| 清流县| 丹寨县| 金门县| 琼结县| 内黄县| 本溪市| 望谟县| 鹤山市| 财经| 宜兴市| 祁门县| 德化县| 明溪县| 嘉定区| 略阳县| 永城市| 资阳市| 龙岩市| 洪洞县| 高雄县| 砚山县| 财经| 耒阳市| 新晃| 枣强县| 兴安县| 阜康市| 会宁县| 长宁区|