Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to save an image from a URL using PHP?

NickFNickF USAMember Posts: 132

I need to save an image taken from a third party url and save it with a different name. Can someone please let me know how to do it with an example?

Tagged:

Comments

  • DavidMDavidM USAMember Posts: 342

    if allow_url_fopen set to true:

    $url = 'http://example.com/image.php';
    $img = '/my/folder/flower.gif';
    file_put_contents($img, file_get_contents($url));
    

    Else use cURL:

    $ch = curl_init('http://example.com/image.php');
    $fp = fopen('/my/folder/flower.gif', 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    
Sign In or Register to comment.