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.

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);
?>

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.

@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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.