What is the best way to download images from a URL using PHP?
To download images from a URL using PHP, you can use the `file_get_contents()` function to retrieve the image data and then use the `file_put_contents()` function to save it to a file on your server. Make sure to validate the URL and handle any errors that may occur during the download process.
$url = 'https://example.com/image.jpg';
$imageData = file_get_contents($url);
if ($imageData !== false) {
file_put_contents('image.jpg', $imageData);
echo 'Image downloaded successfully.';
} else {
echo 'Failed to download image.';
}
Related questions
-
file_get_contents fopen cURL
-
session_start session variables PHP
-
PHP 7.4 PHP 8.1 backward compatibility