PHP download remote files and get file contents

Source: Internet
Author: User
Tags php download save file

/**     *  get the contents of a remote file      *  @parma   $url    URL     *  @return   Get content      * **/     public static function getcontent ($url) {$flag  = 0;do {$hCurl  = curl_init (); curl_setopt ($hCurl,  curlopt_header, 0); curl_setopt ($hCurl,  curlopt_ returntransfer, 1); curl_setopt ($hCurl,  curlopt_timeout, 120); curl_setopt ($hCurl,  CURLOPT_ connecttimeout, 4) curl_setopt ($hCurl, curlopt_url,  $url); $data  = curl_exec ($hCurl); if  (Curl_errno ($hCurl))  {break;} $http _code = curl_getinfo ($hCurl,  curlinfo_http_code);if  ($http _code >= 400)  { //400 - 600 is server errorbreak;} $flag  = 1;}  while  (0); Curl_close ($hCurl);if  ($flag)  {return  $data;}  else {return&nBsp;false;}} /** *  download remote File save local file  *  @parma   $fileName   Save file  *  @parma   $url    URL     *  @return   Get content  * **/public static  Function getcontenttofile ($fileName,  $url) {$out  = fopen ($fileName,  ' W ');if  ( Empty ($out))  {return false;} $flag  = 0;do {$hCurl  = curl_init (); curl_setopt ($hCurl,  curlopt_header, 0); curl_setopt ($hCurl,  curlopt_returntransfer, 1); curl_setopt ($hCurl,  curlopt_timeout, 120); curl_setopt ($hCurl,  curlopt_connecttimeout, 4); curl_setopt ($hCurl, curlopt_file,  $out); curl_setopt ($hCurl, curlopt_url,  $url), $data  = curl_exec ($hCurl);if  (Curl_errno ($ Hcurl))  {break;} $http _code = curl_getinfo ($hCurl,  curlinfo_http_code);if  ($http _code >= 400)  { //400 - 600 is server errorbreak;} $flag = 1;}  while  (0); Curl_close ($hCurl); fclose ($out);if  ($flag)  {return  $data;}  else {return false;}}


This article is from the "dream to think XI" blog, please be sure to keep this source http://qiangmzsx.blog.51cto.com/2052549/1618903

PHP download remote files and get file contents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.