Jump to content

Save Images Using cURL


vin_akleh

Recommended Posts

this script saves the image from a url to the folder were this script is saved, how can i make this code save images into specific folder such as "/image/poster_image/"

<?php
$img[]='http://images.rottentomatoescdn.com/images/redesign/poster_default.gif';

foreach($img as $i){
echo $i;
    save_image($i); 
    // if(getimagesize(basename($i))){
        // echo '<h3 style="color: green;">Image ' . basename($i) . ' Downloaded OK</h3>';
    // }else{
        // echo '<h3 style="color: red;">Image ' . basename($i) . ' Download Failed</h3>';
    // }
}

//Alternative Image Saving Using cURL seeing as allow_url_fopen is disabled - bummer
function save_image($img,$fullpath='basename'){
    if($fullpath=='basename'){
        $fullpath = basename($img);
    }
    $ch = curl_init ($img);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $rawdata=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($fullpath)){
        unlink($fullpath);
    }
    $fp = fopen($fullpath,'x');
    fwrite($fp, $rawdata);
    fclose($fp);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.