A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearSaving image from PHP URL - page 1
User InfoPosts
Saving image from PHP URL#1
I need to save an image from a PHP URL to my PC.
Let(s say I have a page, http://example.com/image.php, holding a single "flower" image, nothing else. How can I save this image from the URL with a new name (using PHP)?

posted date: 2009-04-06 22:50:00

Re: Saving image from PHP URL#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-06 22:50:01

Re: Saving image from PHP URL#3
$content = file_get_contents((http://example.com/image.php();file_put_contents((/my/folder/flower.jpg(, $content);

posted date: 2009-04-06 22:55:00

Re: Saving image from PHP URL#4
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?

posted date: 2009-04-06 23:07:00

Re: Saving image from PHP URL#5
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.

posted date: 2009-04-06 23:12:00

Re: Saving image from PHP URL#6
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);

posted date: 2009-04-06 23:15:00

Re: Saving image from PHP URL#7
Perhaps the site admin has forbidden outside referrals. In that case you can try stream_context_create() and set the appropriate HTTP headers. us2.php.net/manual/en/function.stream-context-create.php

posted date: 2009-04-06 23:18:00

Re: Saving image from PHP URL#8
urlencode('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.

posted date: 2009-04-06 23:19:00

Re: Saving image from PHP URL#9
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();

posted date: 2009-04-07 00:00:00

Re: Saving image from PHP URL#10
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 (“example.com/image.php”) then our script automatically fetch the new image and store it to my directory?

posted date: 2009-04-07 00:26:00

Re: Saving image from PHP URL#11
And how do you know, that the new image "came"?

posted date: 2009-04-07 00:37:00

Re: Saving image from PHP URL#12
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']);.

posted date: 2009-11-29 05:04:00

select page: « 1 2...»
Copyright ©2008-2017 www.momige.com, all rights reserved.