PHP實(shí)現(xiàn)遠(yuǎn)程下載文件到本地
代碼很簡(jiǎn)單就不多廢話(huà)了,直接奉上:
<?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+MySql實(shí)現(xiàn)一個(gè)簡(jiǎn)單的留言板
留言板是接觸WEB開(kāi)發(fā)的基礎(chǔ),寫(xiě)一個(gè)留言板需要知道前端的一些基礎(chǔ)標(biāo)簽,對(duì)數(shù)據(jù)庫(kù)有一個(gè)了解會(huì)基礎(chǔ)SQL語(yǔ)言,PHP基礎(chǔ)知識(shí),前段基礎(chǔ)+數(shù)據(jù)庫(kù)基礎(chǔ)+PHP基礎(chǔ)=>留言板2020-07-07
php實(shí)現(xiàn)mysql封裝類(lèi)示例
這篇文章主要介紹了php實(shí)現(xiàn)mysql封裝類(lèi)示例,需要的朋友可以參考下2014-05-05
php的curl攜帶header請(qǐng)求頭信息實(shí)現(xiàn)http訪問(wèn)的方法
這篇文章主要介紹了php的curl攜帶header請(qǐng)求頭信息實(shí)現(xiàn)http訪問(wèn)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
php微信公眾號(hào)開(kāi)發(fā)之校園圖書(shū)館
這篇文章主要為大家詳細(xì)介紹了php微信公眾號(hào)開(kāi)發(fā)之校園圖書(shū)館,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
thinkPHP簡(jiǎn)單導(dǎo)入和使用阿里云OSSsdk的方法
這篇文章主要介紹了thinkPHP簡(jiǎn)單導(dǎo)入和使用阿里云OSSsdk的方法,簡(jiǎn)單說(shuō)明了阿里云OSS的php sdk下載地址及thinkPHP導(dǎo)入與使用OSSsdk的方法,需要的朋友可以參考下2017-03-03
PHP實(shí)現(xiàn)手機(jī)歸屬地查詢(xún)API接口實(shí)現(xiàn)代碼
主要使用curl實(shí)現(xiàn),需要開(kāi)啟php對(duì)curl的支持2012-08-08
PHP小白必須要知道的php基礎(chǔ)知識(shí)(超實(shí)用)
PHP是一 種被廣泛應(yīng)用的開(kāi)放源代碼的、基于服務(wù)器端的用于產(chǎn)生動(dòng)態(tài)網(wǎng)頁(yè) 的、可嵌入HTML中的腳本程序語(yǔ)言,尤其適合 WEB 開(kāi)發(fā)。下面給大家分享PHP小白必須要知道的php基礎(chǔ)知識(shí),超實(shí)用,感興趣的朋友一起學(xué)習(xí)吧2017-10-10

