如何将远程图像复制到我的网站目录?

Gal*_*Gal 2 php image

我发布了来自其他网站的图片,我宁愿在我的服务器上安装这些图片,以防他们的服务器突然死亡.假设该文件位于"www.www.www/image.gif",如何安全地将其复制到我的目录"images"?

我用PHP写.

谢谢!

Sam*_*son 8

以下应该有效:

// requires allow_url_fopen
$image = file_get_contents('http://www.url.com/image.jpg');
file_put_contents('/images/image.jpg', $image);
Run Code Online (Sandbox Code Playgroud)

或者cURL路线:

$ch = curl_init('http://www.url.com/image.jpg');
$fp = fopen('/images/image.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Run Code Online (Sandbox Code Playgroud)