Phpcurl and fopen download remote server image instances

Source: Internet
Author: User
Tags file url
This article describes how to download the image instance code of a remote server from phpcurl and fopen. For more information, see. The fopen function code is as follows: Copy the code? Php...

This article introduces php curl and fopen remote server image download instance code. For more information, see.

Fopen function

The code is as follows:

Function get_file ($ url, $ folder, $ pic_name ){
Set_time_limit (24*60*60); // you can specify the maximum execution time.
$ Destination_folder = $ folder? $ Folder. '/': ''; // file download and save Directory
$ Newfname = $ destination_folder. $ pic_name; // file PATH
$ File = fopen ($ url, 'RB ');

If ($ file ){
$ Newf = fopen ($ newfname, 'WB ');
If ($ newf ){
While (! Feof ($ file )){
Fwrite ($ newf, fread ($ file, 1024*8), 1024*8 );
}
}
If ($ file ){
Fclose ($ file );
}
If ($ newf ){
Fclose ($ newf );
}
}
}
Get_file ("/img/baidu_logo.gif", "file", "baidu.jpg ");
?>

Php curl function

The code is as follows:
/*
* Function: php supports multiple methods to download remote images and save them to a local device.
* Parameter: file url, saving file name, used for download
* When the name of the saved file is null, the original name of the remote file is used.
*/
Function getImage ($ url, $ filename = '', $ type = 0 ){
If ($ url = '') {return false ;}
If ($ filename = ''){
$ Ext = strrchr ($ url ,'.');
If ($ ext! Using '.gif '& $ ext! Else '.jpg ') {return false ;}
$ Filename = time (). $ ext;
}
// File storage path
If ($ type ){
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ Img = curl_exec ($ ch );
Curl_close ($ ch );
} Else {
Ob_start ();
Readfile ($ url );
$ Img = ob_get_contents ();
Ob_end_clean ();
}
$ Size = strlen ($ img );
// File size
$ Fp2 = @ fopen ($ filename, 'A ');
Fwrite ($ fp2, $ img );
Fclose ($ fp2 );
Return $ filename;
}


To enable the php curl function library

1) remove the file in windows/php. ini; prefix extension = php_curl.dll;/* use echo phpinfo () to view the php. ini path */

2) copy php5/libeay32.dll and ssleay32.dll to the system directory windows/

3) restart apache



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.