PDA

View Full Version : fwrite - PHP



Jam-ez
28-11-2008, 08:16 PM
Hi,

I currently have a script which writes to a file called pollinfo.php;


<?php
$vote = $_POST['group1'];
if ($vote == '1')
{
$vote = 'fifth';
$file = @fopen("pollinfo.php", "w");
$data = "\nOne vote for the $vote \n";
@fwrite($file, $data);
@fclose($file);
}
else if ($vote == '2')
{
$vote = 'thirteenth';
$file = @fopen("pollinfo.php", "w");
$data = "\nOne vote for the $vote \n";
@fwrite($file, $data);
@fclose($file);
}

echo("<br /><br />Thank you for sending in your opinion!");

?>

However, every time someone votes, the poll overwrites the current data. Is there anyway to keep the current data and just continue as a new set of data, or would I have to open the file, read what it says, and add it to the $data variable?

Regards,
James.

Source
28-11-2008, 08:27 PM
Why don't you use fopen to open the file, grab the data, and then append the new poll vote onto the end of the current information? You could store it like this

1|0|1|0|0|1|1

then explode it, and count the array for the total? I dunno, but you definatly need to use fopen.

Jam-ez
28-11-2008, 08:31 PM
Why don't you use fopen to open the file, grab the data, and then append the new poll vote onto the end of the current information? You could store it like this

1|0|1|0|0|1|1

then explode it, and count the array for the total? I dunno, but you definatly need to use fopen.

Yeah, that was my thought exactly.. Well thanks for your help, I'll try to fopen it store the data already in the file then replace the data with the additional data.

Jam-ez
28-11-2008, 08:53 PM
Sorry for double post, but I've just found something called "Append (a)";


Append: 'a'
Open a file for write only use. However, the data in the file is preserved and you begin will writing data at the end of the file. The file pointer begins at the end of the file.

A file pointer is PHP's way of remembering its location in a file. When you open a file for reading, the file pointer begins at the start of the file. This makes sense because you will usually be reading data from the front of the file.

However, when you open a file for appending, the file pointer is at the end of the file, as you most likely will be appending data at the end of the file. When you use reading or writing functions they begin at the location specified by the file pointer.

Jxhn
29-11-2008, 08:24 AM
Sorry for double post, but I've just found something called "Append (a)";

Yes, that's what o need.

@fopen("pollinfo.php", "a");

Want to hide these adverts? Register an account for free!