保存图像从url与curl PHP [英] Save image from url with curl PHP

查看:86
本文介绍了保存图像从url与curl PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用CURL从网址保存图片,并将其保存到我的服务器上的文件夹。我一直在与这个代码无法使用。理想情况下,我想抓住图像,并将其保存为photo1或某物。帮帮我!

I need to save an image from a url using CURL and save it to a folder on my server. I've been battling with this code to no avail. Ideally I'd like to grab the image and save it as "photo1" or something. Help!

    function GetImageFromUrl($link)

    {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_POST, 0);

    curl_setopt($ch,CURLOPT_URL,$link);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result=curl_exec($ch);

    curl_close($ch);

    return $result;

    }

    $sourcecode = GetImageFromUrl($iticon);

    $savefile = fopen(' /img/uploads/' . $iconfilename, 'w');
    fwrite($savefile, $sourcecode);
    fclose($savefile);


推荐答案

请尝试:

function grab_image($url,$saveto){
    $ch = curl_init ($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $raw=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}

并确保在php.ini中allow_url_fopen is enable

and ensure that in php.ini allow_url_fopen is enable

这篇关于保存图像从url与curl PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆