0

what's up?

I have the following php code:

<?php

//chose the file
$file = 'C:\Program Files\EasyPHP-5.3.2i\www\test\dump.txt';

//open the file in writeable mode
$data = fopen($file, 'w');
//add the new element to it
fwrite($data, $data.'gagi');
//close the file
fclose($data);
?>

I expect it to keep whatever is in the file and add 'gagi' (without quotes) to the file i referenced in $file. however, what it does is: it deletes everything in the file and puts in it the following: "Resource id #3gagi".

specifically, what I am trying to do is build a script that keeps track of when people sign in on a site (or network) that I control. something like a log file.

any help is much appreciated!

P.S. I also tried reading the file as an array, and then use array_push(), but it aint worked either.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by bibiki
1

Try the following:

<?php
$file = "<your_path_to_file>";
// Open the file in APPEND mode.
// This will put the cursor at the end of the file.
$fp = fopen($file, 'a');
// Write data to it.
fwrite($fp, 'gagi');
// Close the file.
fclose($fp);
?>
0

If you don't have a copy of the PHP manual, you need to download a copy and use it. It states very clearly:
'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

You then see option 'a':
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

0

@sergb, thanks a whole lot. that worked ideally.
@chrishea, thanks for taking your time. I do have the manual but find it difficult to understand. thanks anyways!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.