Download image using php from protected server

If you want to download image from a website where allow_url_fopen set to false or some other protection applied then you can’t download it with file_put_content() or copy() etc. Using curl and behaving like a user agent can be used to download image to local directory.

$img ='http://media.example.com/product/428x571/880.jpg'
$newinsrc = 'images/'.time().'.jpg';
$ch = curl_init($img);
$fp = fopen($newinsrc, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_exec($ch);
curl_close($ch);
fclose($fp);