curl下载远程图片到服务器

 php
//curl下载远程图片到服务器 方法  
     function download($url, $path = 'images/'){
    $ch = curl_init();
    $names = date('Y-m-d',time());
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    $file = curl_exec($ch);
    curl_close($ch);
    $filename = pathinfo($url, PATHINFO_BASENAME);
    $lujing = $path . $names;
    if(!is_dir($lujing)){
        mkdir(iconv("UTF-8", "GBK", $lujing),0777,true);
    } 
    //$resource = fopen($path . $names.'/'.$filename, 'a');
   $info =  file_put_contents($path . $names.'/'.$filename,$file);
   if($info){
       return $path . $names.'/'.$filename;
   }
    
    //fwrite($resource, $file);
    //fclose($resource);
  }

 

你可能感兴趣的:(curl下载远程图片到服务器)