views:

19757

answers:

4

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
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
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
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
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
thanks for the urlencode() hint. As for the binary flag: That one is available in PHP 6 only.
soulmerge
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
This is another question and it had been asked some days ago on SO, just try to search it.
soulmerge
+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
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
And how do you know, that the new image "came"?
vartec
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
+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
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