Saving image from PHP URL

后端 未结 12 1958
日久生厌
日久生厌 2020-11-21 17:10

I need to save an image from a PHP URL to my PC. Let\'s say I have a page, http://example.com/image.php, holding a single \"flower\" image, nothing else. How ca

12条回答
  •  野的像风
    2020-11-21 17:51

    If you have 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);
    

提交回复
热议问题