当前位置 : 主页 > 网络编程 > PHP >

处理远程图片下载

来源:互联网 收集:自由互联 发布时间:2021-06-28
处理远程图片下载 //处理远程图片下载//$url格式为 $url = 'https://img.weixiaoqu.com/images/uploads/201708/7db23487c0c16bbb642afe9a6e2ab061.jpg';function download_img($url){ $file_path =dirname($_SERVER['SCRIPT_FILENAME'])."
处理远程图片下载
//处理远程图片下载
//$url格式为   $url = 'https://img.weixiaoqu.com/imageshttp://img.558idc.com/uploadfile/201708/7db23487c0c16bbb642afe9a6e2ab061.jpg';
function download_img($url)
{
    $file_path =dirname($_SERVER['SCRIPT_FILENAME'])."/upload/sq/".basename($url);
    if(!file_exists($file_path)){
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        ob_start ();
        curl_exec ( $ch );
        $return_content = ob_get_contents ();
        ob_end_clean ();
        $return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
        $fp= @fopen($file_path,"a"); //将文件绑定到流
        fwrite($fp,$return_content); //写入文件
        fclose($fp);
        return $file_path;
    }else{
        return $file_path;
    }
}
网友评论