Download File to server from URL

后端 未结 12 1215
旧巷少年郎
旧巷少年郎 2020-11-22 04:05

Well, this one seems quite simple, and it is. All you have to do to download a file to your server is:

file_put_contents(\"Tmpfile.zip\", file_get_contents(\         


        
相关标签:
12条回答
  • Use a simple method in php copy()

    copy($source_url, $local_path_with_file_name);
    

    Note: if the destination file already exists, it will be overwritten

    PHP copy() Function

    Note: You need to set permission 777 for the destination folder. Use this method when you are downloading to your local machine.

    Special Note: 777 is a permission in Unix based system with full read/write/execute permission to owner, group and everyone. In general we give this permission to assets which are not much needed to be hidden from public on a web server. Example: images folder.

    0 讨论(0)
  • 2020-11-22 04:29
    set_time_limit(0); 
    $file = file_get_contents('path of your file');
    file_put_contents('file.ext', $file);
    
    0 讨论(0)
  • 2020-11-22 04:32

    I use this to download file

    function cURLcheckBasicFunctions()
    {
      if( !function_exists("curl_init") &&
          !function_exists("curl_setopt") &&
          !function_exists("curl_exec") &&
          !function_exists("curl_close") ) return false;
      else return true;
    }
    
    /*
     * Returns string status information.
     * Can be changed to int or bool return types.
     */
    function cURLdownload($url, $file)
    {
      if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions";
      $ch = curl_init();
      if($ch)
      {
    
        $fp = fopen($file, "w");
        if($fp)
        {
          if( !curl_setopt($ch, CURLOPT_URL, $url) )
          {
            fclose($fp); // to match fopen()
            curl_close($ch); // to match curl_init()
            return "FAIL: curl_setopt(CURLOPT_URL)";
          }
          if ((!ini_get('open_basedir') && !ini_get('safe_mode')) || $redirects < 1) {
            curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            //curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
            if( !curl_setopt($ch, CURLOPT_HEADER, $curlopt_header)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
            if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $redirects > 0)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
            if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
            if( !curl_setopt($ch, CURLOPT_MAXREDIRS, $redirects) ) return "FAIL: curl_setopt(CURLOPT_MAXREDIRS)";
    
            return curl_exec($ch);
        } else {
            curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            //curl_setopt($ch, CURLOPT_REFERER, 'http://domain.com/');
            if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false)) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
            if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
            if( !curl_setopt($ch, CURLOPT_HEADER, true)) return "FAIL: curl_setopt(CURLOPT_HEADER)";
            if( !curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)) return "FAIL: curl_setopt(CURLOPT_RETURNTRANSFER)";
            if( !curl_setopt($ch, CURLOPT_FORBID_REUSE, false)) return "FAIL: curl_setopt(CURLOPT_FORBID_REUSE)";
            curl_setopt($ch, CURLOPT_USERAGENT, '"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.11) Gecko/20071204 Ubuntu/7.10 (gutsy) Firefox/2.0.0.11');
        }
          // if( !curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true) ) return "FAIL: curl_setopt(CURLOPT_FOLLOWLOCATION)";
          // if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)";
          // if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)";
          if( !curl_exec($ch) ) return "FAIL: curl_exec()";
          curl_close($ch);
          fclose($fp);
          return "SUCCESS: $file [$url]";
        }
        else return "FAIL: fopen()";
      }
      else return "FAIL: curl_init()";
    }
    
    0 讨论(0)
  • 2020-11-22 04:34

    There are 3 ways:

    1. file_get_contents and file_put_contents
    2. CURL
    3. fopen

    You can find examples from here.

    0 讨论(0)
  • 2020-11-22 04:35

    prodigitalson's answer didn't work for me. I got missing fopen in CURLOPT_FILE more details.

    This worked for me, including local urls:

    function downloadUrlToFile($url, $outFileName)
    {   
        if(is_file($url)) {
            copy($url, $outFileName); 
        } else {
            $options = array(
              CURLOPT_FILE    => fopen($outFileName, 'w'),
              CURLOPT_TIMEOUT =>  28800, // set this to 8 hours so we dont timeout on big files
              CURLOPT_URL     => $url
            );
    
            $ch = curl_init();
            curl_setopt_array($ch, $options);
            curl_exec($ch);
            curl_close($ch);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 04:35

    best solution

    install aria2c in system &

     echo exec("aria2c \"$url\"")
    
    0 讨论(0)
提交回复
热议问题