Page 1 of 1
Capture image from a website and saving it to my own server.
Posted: Sat Jun 24, 2006 10:30 pm
by edliu
Hi Guys,
I have a small problem here. I have the address of the image on the web such as "<img src = "
http://abc.com/testing4jh3e.jpg".
I managed to extract the address of the image src as it is constantly changing. However, I wish to be able to download the image using php in to my web server root directoy so that I can use php again to process the image such as resize etc...I tried file functions an image functions but due to the fact that I am still a newbie, I am not able to comprehend how it works...Thus requesting advice from you guys.....
Thanks a lot!

Posted: Sat Jun 24, 2006 10:41 pm
by feyd
provided you have permission to pull the image from their server,
file_get_contents() can often be used to retrieve the file's binary data.
file_put_contents() or a combination of
fopen()-
fwrite()-close() can write the file to your server.
Posted: Sun Jun 25, 2006 12:55 am
by edliu
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="feyd"]provided you have permission to pull the image from their server, [url=http://php.net/function.file_get_contents]file_get_contents()[/url] can often be used to retrieve the file's binary data. [url=http://php.net/function.file_put_contents]file_put_contents()[/url] or a combination of [url=http://php.net/function.fopen]fopen()[/url]-[url=http://php.net/function.fwrite]fwrite()[/url]-close() can write the file to your server.[/quote]
Can you provide me an example? Thanks. However, here is my code:
Code: Select all
/*=========================*/
$onlyimage = 'http://www.onemotoring.com.sg/trafficsmart/images/2701_1412_20060625141115_f01bb5.jpg"
$somecontent= file_get_contents($onlyimage);
if (fwrite("/home/edmundlx/pics/traffic/abc.jpg", $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
/*=============================*/
It gives me error....cannot write to file..
Pls guide me .Thanks
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Sun Jun 25, 2006 6:32 am
by ok
fwrite isn't properly written...
You have to give it a resource handle: fwrite ( resource handle, string string).
Code: Select all
//...
$handle = fopen("/home/edmundlx/pics/traffic/abc.jpg");
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ("/home/edmundlx/pics/traffic/abc.jpg")";
exit;
}