最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP實(shí)現(xiàn)遠(yuǎn)程下載文件到本地

 更新時(shí)間:2015年05月17日 10:37:38   投稿:hebedich  
經(jīng)常寫(xiě)采集器發(fā)布接口需要使用到遠(yuǎn)程附件的功能,所以自己寫(xiě)了一個(gè)PHP遠(yuǎn)程下載文件到本地的函數(shù),一般情況下已經(jīng)夠用了,如果服務(wù)器支持CURL函數(shù),程序則會(huì)優(yōu)先選擇CURL,有需要的小伙伴可以參考下。

代碼很簡(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)文章

最新評(píng)論

呼图壁县| 赤壁市| 仙居县| 田林县| 郎溪县| 定州市| 漠河县| 阿瓦提县| 兴城市| 从江县| 牙克石市| 拉孜县| 淮南市| 石景山区| 南乐县| 普兰店市| 汉川市| 甘南县| 海阳市| 新田县| 永吉县| 拜泉县| 五大连池市| 峨眉山市| 花莲县| 万载县| 宝兴县| 平泉县| 调兵山市| 南京市| 齐齐哈尔市| 平阴县| 广饶县| 本溪市| 喀喇| 饶阳县| 九江市| 永和县| 吉木萨尔县| 青阳县| 敖汉旗|