从 PHP 网址保存图像

2022-08-30 05:53:43

我需要将图像从 PHP URL 保存到我的电脑。假设我有一个页面,拿着一个“花”图像,没有别的。如何从具有新名称(使用PHP)的URL中保存此图像?http://example.com/image.php


答案 1

如果已设置为 :allow_url_fopentrue

$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);

答案 2

使用 PHP 的函数 copy()

copy('http://example.com/image.php', 'local/folder/flower.jpg');

注意:这需要allow_url_fopen


推荐