fopen、file_get_content、fsocket、curl下载图片

直接用fopen下载远程图片

$img=fopen("./mao2.png","w+");
fwrite($img, "http://www.php100.com/uploadfile/2015/0507/thumb_420_240_20150507105744216.png");

使用file_get_content直接下载远程图片

$mao=file_get_contents("http://www.php100.com/uploadfile/2015/0507/thumb_420_240_20150507105744216.png");

file_put_contents("./mao3.png",$mao);

使用fsocket

$headle=fsockopen("www.php100.com",80,$errno,$errstr,30);
 if(!$headle)
 {
    echo "$errstr($errno)<br/>\n";
 }else
 {
    $out="GET /uploadfile/2015/0507/thumb_420_240_20150507105744216.png HTTP/1.1\r\n";
    $out.="Host: www.php100.com\r\n";
    $out.="Connection: Close\r\n\r\n";
    $mao='';
    fwrite($headle,$out);//写入
    while (!feof($headle)) {
        $mao .=fgets($headle,1024);
    }


    $arr=explode("\r\n\r\n",$mao); //截取返回的重要信息
    header("Content-type:image/png");//直接向浏览器输出图片
    echo $arr[1];
    //fwrite(fopen("./mao4.png","w+"), $arr[1]);//保存图片

curl

初始化curl
$php100=curl_init();
curl_setopt($php100,CURLOPT_URL, "http://www.php100.com/uploadfile/2015/0507/thumb_420_240_20150507105744216.png");
curl_setopt($php100, CURLOPT_RETURNTRANSFER, 1);
curl_exec($php100);
$php=curl_multi_getcontent($php100);
curl_close($php100);
$img=fopen('./mao.png',"w+");
fwrite($img,$php);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值