Hi, i need to save a image from a php URL to my pc. Let have a page "http://example.com/image.php" holding a single "flower" image, nothing else. How can i save this image from URL with a new name? [Using PHP] Pls help.
+4
A:
$content = file_get_contents('http://example.com/image.php');
file_put_contents('/my/folder/flower.jpg', $content);
soulmerge
2009-04-07 06:55:10
The page is holding an animated gif image. A file is stored into the folder as flower.gif .But it is blank.No image show.any solution?
riad
2009-04-07 07:07:47
Turn on error_reporting(E_ALL|E_STRICT) and check the return value of file_get_contents(), then you should get a reasonable error message.
soulmerge
2009-04-07 07:12:19
Perhaps the site admin has forbidden outside referrals. In that case you can try stream_context_create() and set the appropriate HTTP headers. http://us2.php.net/manual/en/function.stream-context-create.php
Calvin
2009-04-07 07:18:36
urlencode('http://example.com/image.php') == 'http%3A%2F%2Fexample.com%2Fimage.php', obviously not what you want. Also file is binary, proper flag needs to be set.
vartec
2009-04-07 07:19:27
thanks for the urlencode() hint. As for the binary flag: That one is available in PHP 6 only.
soulmerge
2009-04-07 07:23:45
Thanks all, it’s working fine now. But can anybody help me to do it automated. I mean when a new image come that URL 'http://example.com/image.php' my script will automatically fetch the image and store it to my directory?
riad
2009-04-07 08:19:58
This is another question and it had been asked some days ago on SO, just try to search it.
soulmerge
2009-04-07 11:58:47
+19
A:
If you have allow_url_fopen
set to true
:
$url = 'http://example.com/image.php';
$img = '/my/folder/flower.gif'
file_put_contents($img, file_get_contents($url));
Else use cURL:
$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);
vartec
2009-04-07 07:15:24
Thanks bro, your code help me to solve the problem. But could u pls help me to make the script automated .I mean when a new gif image come to the url (“http://example.com/image.php”) then our script automatically fetch the new image and store it to my directory?
riad
2009-04-07 08:26:55
I think riad means using a `$_GET` variable containing the URL of the image `http://example.com/fetch-image.php?url=http://blabla.com/flower.jpg`. In the case of this example, you could just call `$_GET['url']` in your PHP script, like so: `$ch = curl_init($_GET['url']);`.
Mathias Bynens
2009-11-29 13:04:58
+3
A:
Here you go, the example saves the remote image to image.jpg.
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');
Sam152
2009-04-07 08:00:15
A:
Thank you very much for all. using these tips and some string functions in php, i created a module for downloading all images from a particular site. i tried a software "Extreme Picture Finder" but it is not able to download images from site using "coppermine photo gallery" and that software also costs $50 but my code is able to download images from those sites too.
Once again thanks :)
boysmakesh
2010-07-31 10:00:58