PHP實(shí)現(xiàn)遠(yuǎn)程下載文件到本地
代碼很簡(jiǎn)單就不多廢話了,直接奉上:
<?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 = stream_context_create($opts); if(@copy($url, $file, $context)) { //$http_response_header return $file; } else { return false; } } } ?>
再來(lái)個(gè)遠(yuǎn)程下載文件到服務(wù)器
<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); } ?>
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
老生常談PHP面向?qū)ο笾畼?biāo)識(shí)映射
下面小編就為大家?guī)?lái)一篇老生常談PHP面向?qū)ο笾畼?biāo)識(shí)映射。小編覺(jué)得挺不錯(cuò)的?,F(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06php實(shí)現(xiàn)zip壓縮文件解壓縮代碼分享(簡(jiǎn)單易懂)
這篇文章主要介紹了php的zip壓縮文件解壓縮實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-05-05php+mongodb判斷坐標(biāo)是否在指定多邊形區(qū)域內(nèi)的實(shí)例
本篇文章主要介紹了php+mongodb判斷坐標(biāo)是否在指定多邊形區(qū)域內(nèi)的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10PHP中用Trait封裝單例模式的實(shí)現(xiàn)
這篇文章主要介紹了PHP中用Trait封裝單例模式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12php實(shí)現(xiàn)監(jiān)聽(tīng)事件
本文介紹關(guān)于在php實(shí)現(xiàn)事件監(jiān)聽(tīng)與觸發(fā)實(shí)例程序代碼2013-11-11PHP函數(shù)分享之curl方式取得數(shù)據(jù)、模擬登陸、POST數(shù)據(jù)
這篇文章主要介紹了curl方式取得數(shù)據(jù)、模擬登陸、POST數(shù)據(jù),需要的朋友可以參考下2014-06-06