PHP Editing and displaying text from a .txt file

I have a file called "views.txt" that contains "0". When the user loads the page, I want the script to rewrite the text document one higher from the last.

Example

<?php
$a = file_get_contents("views.txt")
$views = $a + 1;
file_put_contents("views.txt",$views);
echo $views;
?>

It will not display anything.

I cannot use MySQL, so I use text files. :)

+3
source share
2 answers

I would suggest that the path to views.txtdoes not apply to the working directory of the file. For security reasons, you should probably specify the full path to the file; those. /path/to/views.txt)

getcwd, , "views.txt" , , getcwd() . "/views.txt".

+4

0\n, \n - . :

<?php
$views_file = "views.txt";
$allviewfile = file($views_file, FILE_IGNORE_NEW_LINES);
$a = $allviewfile[0];
$views = $a + 1;
file_put_contents($views_file,$views);
echo $views;
?>
0

All Articles