PHP Instance code: Implement remote download file to local function _php tutorial

Source: Internet
Author: User
Often write collector publishing interface need to use the function of remote attachment, so I wrote a php remote download file to local function, generally enough, if the server supports the Curl function, the program will prefer curl, Because the test results show that Curl's response time and resource occupancy rate is much smaller than file_get_contents; if you have good suggestions and improvement programs, please leave a message to me Oh!

Code:

Copy to ClipboardWhat to refer to: [www.bkjia.com] 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;
}
}
}
?>

http://www.bkjia.com/PHPjc/363836.html www.bkjia.com true http://www.bkjia.com/PHPjc/363836.html techarticle often write collector publishing interface needs to use the function of remote attachment, so I wrote a php remote download file to the local function, generally enough, if the server ...

  • Related Article

    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.