发布日期:2018-03-26
如何在PHP中使用CURL下载文件?+ 查看更多
如何在PHP中使用CURL下载文件?
+ 查看更多
发布日期:2017-04-18 17:48
分类:PHP
浏览次数:82
我需要将图像从PHP URL保存到我的电脑。比方讲,我有一个网页http://example.com/image.php,里面就只有一个单独的“花”的图片,我怎样才能用PHP以一个新的名称将图片从URL地址上保存下来?
回答:
如果你有allow_url_fopen将它设置成true:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif';
file_put_contents($img, file_get_contents($url));
或者用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);