PHP: Copy image from remote server to local server

You might want to copy an image (or any other file) with PHP from a remote location to your local server and store it in a specific location. You can use the PHP file_get_contents() function to achieve this. Here is a code sample:

$remoteimage = "http://domain.com/path/to/remote/image.jpg";
$localimage = "/home/domain/httpdocs/path/to/my/image.jpg";

if ($content = file_get_contents($remoteimage)) {
    if (!empty($content)) {
        $fp = fopen($localimage, "w");
        fwrite($fp, $content);
        fclose($fp);
    }
}

You can get the code here: Copy image from remote server to local server.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: