保存从PHP URL获取的图片
保存从PHP URL获取的图片
我需要将一个PHP URL中的图片保存到我的电脑上。
假设我有一个页面http://example.com/image.php
,只有一个“flower”图像,没有其他信息。如何使用PHP将此图片从URL保存为新名称?
admin 更改状态以发布 2023年5月22日
使用PHP的函数copy():
copy('http://example.com/image.php', 'local/folder/flower.jpg');
注意:需要开启allow_url_fopen配置项
如果您的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);