PHP Save Images Using cURL
March 18th, 2008Read More curl, php
Some hosts disabled the ini setting allow_url_fopen. This also means that the ability to easily grab images by calling imagecreatefromjpeg($img) where $img is a url for an external image does not work.
This is a shame as this technique is pretty easy..
PHP:
-
$remote_img = 'http://www.somwhere.com/images/image.jpg';
-
$img = imagecreatefromjpeg($remote_img);
-
$path = 'images/';
-
imagejpeg($img, $path);
However I was tearing my hair out trying to get a product feed integration system to work on a host that has disabled the allow_url_fopen mechanism which meant the above wouldnt work.
Thankfully cURL was still working. So here is the function I made for grabbing and saving an image using cURL instead:
PHP:
-
//Alternative Image Saving Using cURL seeing as allow_url_fopen is disabled - bummer
-
function save_image($img,$fullpath){
-
$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);
-
}
-
}
Another Problem Solved ![]()











































