PHP Download remote pictures of several methods of explanation

Source: Internet
Author: User
Tags php download
This article mainly introduces the PHP download remote pictures of several ways to summarize the relevant information, the need for friends can refer to the following

Summary of several methods for downloading remote images in PHP

This article shows 3 ways to download pictures from a remote URL and save them to a local file, including File_get_contents,curl and fopen.

1. Using file_get_contents

function Dlfile ($file _url, $save _to) {$content = file_get_contents ($file _url); File_put_contents ($save _to, $content);}

2. Using Curl

function Dlfile ($file _url, $save _to) {$ch = Curl_init (); curl_setopt ($ch, curlopt_post, 0);  curl_setopt ($ch, Curlopt_url, $file _url);  curl_setopt ($ch, Curlopt_returntransfer, 1);  $file _content = curl_exec ($ch); Curl_close ($ch); $downloaded _file = fopen ($save _to, ' W '); Fwrite ($downloaded _file, $file _content); Fclose ($downloaded _file);}

3. Using fopen

function Dlfile ($file _url, $save _to) {$in =  fopen ($file _url, "RB"), $out =  fopen ($save _to, "WB"), while ($chunk = Fread ($in, 8192)) {fwrite ($out, $chunk, 8192);} fclose ($in); Fclose ($out);}

The above is the whole content of this article, I hope that everyone's study has helped.


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.