php

How to save image in directory from URL

The following code snippet helps you to copy an image file from remote URL and save in a folder using PHP.

Make sure that you have write permission to the directory where you want to store the image; to make the folder writable you could do this:

$downloadImagePath='http://www.aboutcity.net/images/aboutcity.png';
    $savePath = $_SERVER['DOCUMENT_ROOT'];
    $ch = curl_init($downloadImagePath);
    $fp = fopen($savePath . '/aboutcity.png', 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    echo "Image Downloaded Successfully in ".$savePath.' Folder.';

Related Articles

Leave a Reply

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

Back to top button