Monday, January 28, 2013

Save image from url to local or remote directory using php



<?php
ini_set(‘max_execution_time’, 180);

function GetImageFromUrl($link)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 0);

curl_setopt($ch,CURLOPT_URL,$link);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result=curl_exec($ch);

curl_close($ch);

return $result;
}

$url=$_POST['txtUrl'];

$html = file_get_contents($url);

$doc = new DOMDocument();
@$doc->loadHTML($html);

$tags = $doc->getElementsByTagName(‘img’);

foreach ($tags as $tag) {

$sanjoy= $tag->getAttribute(‘jqimg’);

if (substr($sanjoy, 0, 5) == ‘/pic/’) {
//echo $abid ;

$ImgName=substr($abid, 5);
$Full_Image_Path=’http://www.pleaserusa.com/pic/&#8217; . $ImgName;
$Imag_Save_Path=’E:/Picture/’.$ImgName;
echo $Full_Image_Path ;
echo ‘<br />’;

$sourcecode=GetImageFromUrl($Full_Image_Path);

$savefile = fopen($Imag_Save_Path, ‘w’);
fwrite($savefile, $sourcecode);
fclose($savefile);
}
}
?>
<a href=”index.html”>Back</a>

No comments:

Post a Comment