Php Remote Image Collection (Anti-leech collection)-PHP source code

Source: Internet
Author: User
Ec (2); variable Description: $ url is the complete url of the remote image and cannot be blank. $ Filenamewww.111cn.net is an optional variable. If it is null, the local file name is automatically generated based on the time and date. & nbsp; the Code is as follows: copy the code script ec (2); script

// Variable description:
// $ Url is the complete url of the remote image. It cannot be blank.
// $ Filename www.111cn.net is an optional variable. If it is null, the local file name is based on the time and date.
// Automatically generated.

The Code is as follows:

Function grabimage ($ url, $ filename = ""){
If ($ url = ""): return false; endif;

If ($ filename = ""){
$ Ext = strrchr ($ url ,".");
If ($ ext! = ". Gif" & $ ext! = ". Jpg"): return false; endif;
$ Filename = date ("dmyhis"). $ ext;
}

Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
$ Size = strlen ($ img );

$ Fp2 = @ fopen ($ filename, "");
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );

Return $ filename;
}

// Change fopen to 111cn.net if the collection prevention function is available

The Code is as follows:

$ Ch = curl_init ();
Curl_setopt ($ ch, curlopt_url, $ url); // This is the target address.
Curl_setopt ($ ch, curlopt_header, 0 );
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_referer, $ referer); // counterfeit
$ Data = curl_exec ($ ch );
Curl_close ($ ch );
Echo $ data;

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.