I am trying to create a simple little script that will fetch a webpage and save it to a file. This is just for practice for the records.
here is my code:
[PHP]
<?php
fwrite(STDOUT,"Created By Derrick Hammer\n");
fwrite(STDOUT,"Copyright 2008 Derrick Hammer\n");
fwrite(STDOUT,"Derrick Hammer.com\n");
fwrite(STDOUT,"\n\nPlease enter the URL you wish to fetch: ");
$url=fgets(STDIN);
fwrite(STDOUT,"Fetching ...\n\n\n");
$data= file_get_contents($url);
fwrite(STDOUT,"What file do you want the data to be saved to? ");
$file= fgets(STDIN);
$f= fopen($file,"w+");
if($f){
fwrite($f,$data);
fclose($f);
fwrite(STDOUT, "Data successfully saved! Press Enter to exit ...");
fgets(STDIN);
}
else{
fwrite(STDOUT, "ERROR! Cound not open file $file . Press Enter to exit ...");
fgets(STDIN);
}
exit(0);
?>
[/PHP]
it always gives me an error. here is the command line output:
Expand|Select|Wrap|Line Numbers
- C:\php5>php "C:\Documents and Settings\Administrator\Desktop\fetcher.php"
- Created By Derrick Hammer
- Copyright 2008 Derrick Hammer
- Derrick Hammer.com
- Please enter the URL you wish to fetch: http://www.google.com
- Fetching ...
- What file do you want the data to be saved to? google.txt
- PHP Warning: fopen(google.txt
- ): failed to open stream: Invalid argument in C:\Documents and Settings\Administ
- rator\Desktop\fetcher.php on line 17
- ERROR! Cound not open file google.txt
- . Press Enter to exit ...
- C:\php5>
(note this is on windows) i do have cygwin, but cygwin and php dont mix well with me.
also, i have tried different file modes and also have use php5's file_put_contents()