小编典典

从PHP URL保存图像

php

我需要将图像从PHP
URL保存到PC。假设我有一个页面,其中仅http://example.com/image.php包含一个“花”图像,仅此而已。如何使用新名称(使用PHP)从URL中保存该图像?


阅读 519

收藏
2020-05-26

共1个答案

小编典典

如果您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);
2020-05-26