By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,534 Members | 2,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,534 IT Pros & Developers. It's quick & easy.

Writing to files in the PHP CLI

P: 5
ok, i probably have not been registered here long, but i am no noob. I am very experienced in php, but the php cli is a bit of new territory for me.

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:


fwrite(STDOUT,"Created By Derrick Hammer\n");
fwrite(STDOUT,"Copyright 2008 Derrick Hammer\n");

fwrite(STDOUT,"\n\nPlease enter the URL you wish to fetch: ");

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+");

fwrite(STDOUT, "Data successfully saved! Press Enter to exit ...");


fwrite(STDOUT, "ERROR! Cound not open file $file . Press Enter to exit ...");


it always gives me an error. here is the command line output:

Expand|Select|Wrap|Line Numbers
  4. C:\php5>php "C:\Documents and Settings\Administrator\Desktop\fetcher.php"
  5. Created By Derrick Hammer
  6. Copyright 2008 Derrick Hammer
  7. Derrick
  10. Please enter the URL you wish to fetch:
  11. Fetching ...
  14. What file do you want the data to be saved to? google.txt
  15. PHP Warning:  fopen(google.txt
  16. ): failed to open stream: Invalid argument in C:\Documents and Settings\Administ
  17. rator\Desktop\fetcher.php on line 17
  18. ERROR! Cound not open file google.txt
  19.  . Press Enter to exit ...
  21. C:\php5>
what the heck is going on???

(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()
Jul 28 '08 #1
Share this Question
Share on Google+
4 Replies

P: 310
You could maybe have two reasons for the error message you are getting.

Firstly, it seems as if you are working on a Windows machine. Try to specify the full path of the file you want to write to. In other words, instead of just "google.txt" try to qualify it with full path including drive letter, e.g. "C:\google.txt". You could do this by prepending a standard directory path stub to whatever file name you type in at the keyboard.

Secondly, you must make certain that the directory that you are trying to write the output file to is "writeable" by the PHP CLI program. That is, let us say you set up a directory that you want to write these files to, like C:\myoutput\, then make sure this directory does not have any security properties that forbids the PHP CLI program from creating files and writing to it.
Jul 28 '08 #2

P: 5
1. tried that via getcwd() function, same result

2. it wouldnt because i created the directory myself and extracted the php to there. No installer. Oh and it does not matter weither the file exists or not, it fails every time. If i cant not do that i might as well give up on cli.
Jul 28 '08 #3

Expert 5K+
P: 5,058
The problem is that all the user input is being cought, including the return character (the Enter key), which makes the path to the file illegal. (You can see this on the error message... note how everything after the file name is shown in a new line)

Try running your file name through trim() before using it.
Jul 28 '08 #4

P: 5
thankyou, it worked.

no all i need to do is decode the urls so i dont have things like %20 which wont be a problem.

Thank you!!!
Jul 28 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.