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:

    $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);
    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