3

I have a file named 'views.txt' which contains '0'. When the user loads the page, I want the script to re-write the text document to 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 won't display anything.

I'm unable to use MySQL, so I'm using text files. :)

4

2 回答 2

4

I would guess that the path to views.txt is not relative to the working directory of the file. For safety reasons, you should probably specify the full path to the file; i.e., /path/to/views.txt).

You can also use getcwd to display the current working directory, so the location of "views.txt" will likely be getcwd() . "/views.txt".

于 2012-04-21T00:21:59.257 回答
0

Your string might really be 0\n, where \n is a newline. Try using:

<?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;
?>
于 2012-04-30T14:04:19.107 回答