Current Location: Home> Latest Articles> How to Download and Save Images from a Remote Link Using PHP

How to Download and Save Images from a Remote Link Using PHP

M66 2025-07-14

How to Download and Save Images from a Remote Link Using PHP

In web development, it's common to need to download images from remote links and store them locally, especially when dealing with user uploads or dynamically generated images. This guide shows you how to use PHP to perform this task and store the image's information in a database.

Download Image Content Using file_get_contents()

First, we use PHP's file_get_contents() function to retrieve the content of the remote image. This function can read a URL and return its content.


$remoteImageUrl = "http://example.com/image.jpg";
$imageContent = file_get_contents($remoteImageUrl);

Save Image to Local Server

Next, we use file_put_contents() to save the image content to a specified path on the local server. To avoid filename conflicts, we can generate a unique filename using the uniqid() function.


$savePath = "/path/to/save/images/";
$filename = uniqid() . ".jpg";
$fileSavePath = $savePath . $filename;
file_put_contents($fileSavePath, $imageContent);

Store Image Information in a Database

To manage image information, it's common practice to store the image's path and other details in a database for easy access later. First, you'll need to create a table in your database to store image information like the ID and path.


CREATE TABLE images (
    id INT PRIMARY KEY AUTO_INCREMENT,
    path VARCHAR(255)
);

Connect to the Database and Insert Image Path

After creating the database table, we use PHP's PDO extension to connect to the database and execute SQL queries. Below is an example of how to connect to the database:


$host = "localhost";
$dbname = "your_database_name";
$username = "your_username";
$password = "your_password";

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
} catch (PDOException $e) {
    die("Failed to connect to database: " . $e->getMessage());
}

Insert Image Information into the Database

Once the database connection is established, we can insert the image's path into the database using the following code:


$query = $pdo->prepare("INSERT INTO images (path) VALUES (:path)");
$query->bindParam(':path', $fileSavePath);
$query->execute();

Retrieve the Image ID

Finally, we use the lastInsertId() method to retrieve the ID of the image that was just inserted into the database and return it.


$imageId = $pdo->lastInsertId();
return $imageId;

Complete Code Example

Below is the complete PHP code example that shows how to download an image from a remote link and return the saved image ID:


$remoteImageUrl = "http://example.com/image.jpg";
$imageContent = file_get_contents($remoteImageUrl);

$savePath = "/path/to/save/images/";
$filename = uniqid() . ".jpg";
$fileSavePath = $savePath . $filename;
file_put_contents($fileSavePath, $imageContent);

$host = "localhost";
$dbname = "your_database_name";
$username = "your_username";
$password = "your_password";

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
} catch (PDOException $e) {
    die("Failed to connect to database: " . $e->getMessage());
}

$query = $pdo->prepare("INSERT INTO images (path) VALUES (:path)");
$query->bindParam(':path', $fileSavePath);
$query->execute();

$imageId = $pdo->lastInsertId();
return $imageId;

With the above code example, you can easily implement the functionality to download and save an image from a remote link and return the saved image ID. You can modify and extend the code based on the specific requirements of your project.