php curl與fopen下載遠程伺服器圖片執行個體

來源:互聯網
上載者:User

fopen函數

 代碼如下 複製代碼

<?php
 function get_file($url,$folder,$pic_name){ 
  set_time_limit(24*60*60); //限制最大的執行時間
  $destination_folder=$folder?$folder.'/':''; //檔案下載儲存目錄
  $newfname=$destination_folder.$pic_name;//檔案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函數

 代碼如下 複製代碼
/*
*功能:php多種方式完美實現下載遠程圖片儲存到本地
*參數:檔案url,儲存檔案名稱,使用的下載方式
*當儲存檔案名稱為空白時則使用遠程檔案原來的名稱
*/
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.