Save Image From URL Using PHP

Code 1: $url = ‘http://example.com/image.php’;
$img = ‘/my/folder/flower.gif’
file_put_contents($img, file_get_contents($url));

Code 2: $ch = curl_init(‘http://example.com/image.php’);
$fp = fopen(‘/my/folder/flower.gif’, ‘wb’);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);


Code 1: $url = ‘http://example.com/image.php’;
$img = ‘/my/folder/flower.gif’
file_put_contents($img, file_get_contents($url));

Code 2: $ch = curl_init(‘http://example.com/image.php’);
$fp = fopen(‘/my/folder/flower.gif’, ‘wb’);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Code 3: $content = file_get_contents(‘http://example.com/image.php’);
file_put_contents(‘/my/folder/flower.jpg’, $content);

Code 4: function save_image($inPath,$outPath)
{ //Download images from remote server
$in= fopen($inPath, “rb”);
$out= fopen($outPath, “wb”);
while ($chunk = fread($in,8192))
{
fwrite($out, $chunk, 8192);
}
fclose($in);
fclose($out);
}
save_image(‘http://www.someimagesite.com/img.jpg’,’image.jpg’);

Leave a Reply

Your email address will not be published. Required fields are marked *