Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File I/O probs

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi .
Im only new to php..Does anyone know how to send multiple parameters to a file for appending. Say if i wanted to write four variables like this..its giving me the following error..'Wrong parameter count for fwrite()'..

$month = date("F");
$day = date("d");
$year = date("y");
$FILE = fopen("stats.txt","a");
fwrite($FILE,$day,$month,$year,$number);
fclose($FILE);

also ..how do i start a line every time the thing writes to the file..like using the \n character in 'C' fwrite($FILE,$day,$month,$year,$number\n);

and how do i leave spaces between the entries??

so it looks like ..

13/8/01 14
14/8/00 15


im working on a simple counter that i want to make like a log file that displays the date, number, browser etc

thanks for your help..

Dave
 
1. To append to the file like that, open the file and store its contents in a variable. Then append the date variables to that variable using the . string concatenation operator. Finally, print the entire string to the file via fwrite();

2. If you open the file and store its contents in the variable $contents (for example), the variable will already contain the escape characters (the \n's and stuff like that), so you needn't worry about that if you follow step 1.

3. Again, if you just pull the contents of the original file out and append a space and the appropriate value to the end, it will work fine.

Hope this helps.
-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Just as vcherubini stated in his post, you can just concatenate your variable.

There are a number of ways to handle file i/o operations, but keep in mind that you always want to make your life as easy as possible.

if you don't really need the month, day, and year separate from each other, you can always use:

$date = date("F d, y")."\n"; // looks like 'July 09, 2001'

You now have the date with a linebreak.

Remember that in order to append data to the file, you need to open the file in the a+ file mode (open (or create if it does not exist) the file and place pointer at end of file)

you now only have to:

fwrite($FILE,$date);

Hope this helps!

Chad.
 
Thanks heaps ..that is exactly the info i was looking for..

cheers

dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top