fopen函数,代码如下:
- <?php
- function get_file($url,$folder,$pic_name){
- set_time_limit(24*60*60);
- $destination_folder=$folder?$folder.'/':'';
- $newfname=$destination_folder.$pic_name;
- $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 getImage($url,$filename='',$type=0){
- if($url==''){return false;}
- if($filename==''){
- $ext=strrchr($url,'.');
- if($ext!='.gif' && $ext!='.jpg'){return false;}
- $filename=time().$ext;
- }
-
- 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);
-
- $fp2=@fopen($filename,'a');
- fwrite($fp2,$img);
- fclose($fp2);
- return $filename;
- }
开启php curl函数库的步骤:
1).去掉windows/php.ini 文件里;extension=php_curl.dll前面的; /*用 echo phpinfo();查看php.ini的路径*/
2).把php5/libeay32.dll,ssleay32.dll复制到系统目录windows/下
3).重启apache |