Page 1 of 1
Fetch and save image
Posted: Sat Apr 08, 2006 3:00 pm
by mshita
Hi,
I'm at a very beginning stages of PHP. So I hope someone can help me with this problem. I'm trying to fetch an image from a Department of Transportation webcam and store it on my server for post processing.
How do I copy the image over?
Obviously copy("http://www.......something.jpg", "local.jpg") doesn't work.
Thank you!
Mounir
Posted: Sat Apr 08, 2006 3:02 pm
by feyd
file_get_contents() if allow_url_fopen is enabled.
fopen()-
fwrite()-
fclose() for creating the file locally.
Posted: Sat Apr 08, 2006 3:11 pm
by mshita
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]
So this is what I tried:
Code: Select all
<?php
$old = "http://www.tripcheck.com/RoadCams/cams/I-5_Millplain_pid771.jpg";
$new = "i5mill.jpg";
$image = substr(file_get_contents($old),0,-1);
$handle = fopen($new, "w");
fwrite($handle, $image);
fclose($handle);
?>
but it gets me the following messages:
Code: Select all
Warning: fopen(i5mill.jpg): failed to open stream: Permission denied in /home/citycamv/public_html/tc/tc.php on line 8
Warning: fwrite(): supplied argument is not a valid stream resource in /home/citycamv/public_html/tc/tc.php on line 9
Warning: fclose(): supplied argument is not a valid stream resource in /home/citycamv/public_html/tc/tc.php on line 10
[/quote]
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: Sat Apr 08, 2006 3:15 pm
by feyd
your script doesn't have write permissions to the directory it is sitting in.
Posted: Sat Apr 08, 2006 3:18 pm
by mshita
DUH! Thank you so much! It works!
Do you know if there is an easy way in PHP to convert jpgs -> png?
Posted: Sat Apr 08, 2006 3:20 pm
by feyd
Posted: Sat Apr 08, 2006 4:33 pm
by mshita
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]
Slightly different problem.
So I wrote a little piece of code that copies from ODOT cams to my server. It works if I execute the code from a browser, but I want to set it up as a CRON job. When I do I get no error messages, but the images are not copied to where I expect to see them (actually I can't find them at all).
Code: Select all
<?php
header("Cache-Control:no-cache");
$conn = mysql_connect("localhost", "mydatabase", "password")
or die("Nope...");
mysql_select_db("mydatabase") or die("<span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>-o");
$sql = "select * from orhwy";
$rs = mysql_query($sql);
while ( $row = mysql_fetch_array($rs) ) {
$old = "http://www.tripcheck.com/RoadCams/cams/".$row["image"];
$new = $row["file"];
$image = substr(file_get_contents($old),0,-1);
$handle = fopen($new, "w");
fwrite($handle, $image);
fclose($handle);
}
echo ("Finished copying");
?>
Any ideas?
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: Sat Apr 08, 2006 4:38 pm
by feyd
you may need to specify the exact directory you want the files to be saved to as the script is likely being run from a different directory than browsing to it would. As for errors, you may want to check the server logs, but it may not show up. You can look into telling PHP to write a log of errors to a certain file via a multitude of ways through the error_log ini directive.
Posted: Sat Apr 08, 2006 4:44 pm
by mshita
Well, this is the message I get
Warning: fopen(/usr/home/citycamv/public_html/tc/i5134wa.jpg): failed to open stream: No such file or directory in /home/citycamv/public_html/tc/tc.php on line 19
Says no such directory in the folder I'm working out of.....doesn't it?[/quote]
Posted: Sat Apr 08, 2006 4:52 pm
by feyd
compare the paths it's showing you in the error.
