Skip to content
PHP Logo

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.

php
$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.';
}