473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Writing to files in the PHP CLI

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:

[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
  1.  
  2.  
  3.  
  4. C:\php5>php "C:\Documents and Settings\Administrator\Desktop\fetcher.php"
  5. Created By Derrick Hammer
  6. Copyright 2008 Derrick Hammer
  7. Derrick Hammer.com
  8.  
  9.  
  10. Please enter the URL you wish to fetch: http://www.google.com
  11. Fetching ...
  12.  
  13.  
  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 ...
  20.  
  21. C:\php5>
  22.  
  23.  
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
4 2231
coolsti
310 100+
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
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
Atli
5,058 Expert 4TB
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
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

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

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
10
by: Neil Trigger | last post by:
Is there a way of creating a seperate text file on a server every time a form is sent? -- ¿ Trigger ? http://www.magic2k.com/ http://www.oddmap.co.uk
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
12
by: Chris Springer | last post by:
I'd like to get some feedback on the issue of storing data out to disk and where to store it. I've never been in a production environment in programming so you'll have to bear with me... My...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: Adonis Vargas | last post by:
I am writing a program that walks a directory full of mp3s reads their ID3 data (using Mutagen), this part works perfectly. The problem is I write these tags to a CSV file through the CSV module....
3
by: koutoo | last post by:
I have a code that writes to 2 seperate files. I keep getting a "list index out of range" error. The strange part is that when checking the files that I'm writing too, the script has already...
20
by: Marin Brkic | last post by:
Hello all, please, let me apologize in advance. English is not my first language (not even my second one), so excuse any errors with which I'm about to embarass myself in front of the general...
2
by: as001 | last post by:
Hi, I'm writing a windows application in C# using VS 2003. I got stuck where it has to write multiple output text files. Here's my piece of code: for loop { Random r = new Random();...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.