Fopen function
| The code is as follows: |
Copy code |
|
<? Php
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: |
Copy code |
/*
* 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