PHP Image Download: 3 Easy Methods

  1. Download images using the file_get_contents() function.
$url = 'https://example.com/image.jpg';
$image = file_get_contents($url);
file_put_contents('image.jpg', $image);
  1. Download images with cURL.
$url = 'https://example.com/image.jpg';
$ch = curl_init($url);
$fp = fopen('image.jpg', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
  1. Download images using the copy() function.
$url = 'https://example.com/image.jpg';
copy($url, 'image.jpg');

Note: When using these methods to download images, make sure the server has sufficient permission to save the files.

bannerAds