Facing problem with Image downloader code from URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manojdp
Forum Newbie
Posts: 1
Joined: Fri Jul 08, 2011 1:05 am

Facing problem with Image downloader code from URL

Post by manojdp »

Hi,
I am new to PHP and I have written this code which downloads image from URL

foreach ($content->find('img[id=prodImage]') as $element){
// echo("FOUND "$element);
$ch = curl_init($element->src);
$filename = parse_url($_POST[$url], PHP_URL_HOST).'_'.time();
echo($filename);
$ext = substr($element->src, -3);
echo($ext);
if(!is_dir('parsed_items'))
mkdir("parsed_items", 0777);
$fp = fopen('parsed_items/'.$filename.'.'.$ext, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}

But whenever I reach curl_exec($ch) the entire contents are dumped on the screen. It works fine at other places. Please guide me as to how can I resolve this or any other API which will let me download the image

Tks & Rgds,
Manoj
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Facing problem with Image downloader code from URL

Post by oscardog »

You'll need to pull apart from result which is given by curl_exec. It will essentially just pull all of the HTML code from the page, so you have to sort through it to get the data you need.
Post Reply