0

Hello guys!


I want to make a viewer function that adds up everytime the script runs.

I want to do it with a text file, something like this i guess..

$fp = fopen("video1.txt", 'a');

        ///I guess i need to get the current value of the file, then add an additional view point somehow.
	fwrite($fp, "What do i do here?  +1 on what?");
	fclose($fp);

I would love you for a thousand years if u wrote this little thing as it should.

/Regards, Sorcher

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Sorcher
0
<?php function incrementCounter($counterFile) { //INCREMENT THE COUNTER
	$current=readCounter($counterFile);
	$current++;
	if(!$handle = fopen($counterFile, "w")) { return false; }
	else { if(fwrite($handle, $current) === FALSE) { return false; } }
	fclose($handle);
	return true; }
function readCounter($counterFile) { //OUTPUT THE CURRENT COUNT
	$contents=file_get_contents($counterFile);
	if(is_numeric((int)$contents)) { return ((int)$contents); }
	else { return 0; } }
$counterFile="video1.txt";
incrementCounter($counterFile); //INCREMENT THE COUNTER
echo readCounter($counterFile); //OUTPUT THE CURRENT COUNT
?>

Edited by almostbob: n/a

0

There is another simpler way to do this:

$content = 'I want to add this line at the end of text file.';
$filePath = 'sometext.txt';

// THIS CODE FIRST OPENS THE FILE, APPENDS CONTENT TO IT AND THEN WRITES IT BACK
file_put_contents($filePath, file_get_contents($filePath) . $content);

Or in your case:

$filePath = 'sometext.txt';

// THIS CODE FIRST OPENS THE FILE, APPENDS CONTENT TO IT AND THEN WRITES IT BACK
file_put_contents($filePath, intval(file_get_contents($filePath)) + 1);

Edited by Todx: n/a

0

Thanks guys! I'll try both options and flag as solved as soon as i got it working, peace

This topic has been dead for over six months. 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.