• 技术文章 >php教程 >php手册

    PHP实现远程下载文件到本地,

    2016-06-13 09:04:12原创359

    PHP实现远程下载文件到本地,


    代码很简单就不多废话了,直接奉上:

    <?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;
        }
      }
    }
    ?>
    

    再来个远程下载文件到服务器

    < ?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); } ?>

    以上所述就是本文的全部内容了,希望大家能够喜欢。

    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:php 远程下载到本地
    上一篇:PHP面向对象之后期静态绑定功能介绍,面向对象功能介绍 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 2015.1写留言板的时用的 知识点和函数,2015.1留言板• ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整,• php操作mysqli(示例代码)• php+ajax导入大数据时产生的问题处理• 最常用的8款 PHP 调试工具,你用过吗?
    1/1

    PHP中文网