PHP 编辑和显示.txt文件中的文本


PHP Editing and displaying text from a .txt file

我有一个名为'views.txt'的文件,其中包含'0'。当用户加载页面时,我希望脚本将文本文档重写到比上一个更高的文档。

例;

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

它不会显示任何内容。

我无法使用MySQL,所以我使用文本文件:)。

我猜views.txt的路径与文件的工作目录无关。 出于安全原因,您可能应该指定文件的完整路径;即/path/to/views.txt)。

你也可以使用 getcwd 来显示当前的工作目录,所以 "views.txt" 的位置可能会getcwd() . "/views.txt"

您的字符串可能真的是 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;
?>