【发布时间】:2013-11-15 00:44:44
【问题描述】:
所以我正在学习 php 并尝试为网页制作一个 php 计数器。现在我已经根据我的理解编写了这段代码。问题是,当我在终端中运行程序时,我得到了正确的答案(输出如我所料)。当我在 html 页面上回显时,它不起作用。这是代码
<html>
<body>
<?php
#open the file (assuming this file already exists with only 0 in it)
$file = 'test.txt';
$file = fopen($file,"r");
$temp = fread($file, 1024);
fclose($file);
#save the number 0 (initial counter to $temp) and delete it)
$file = 'test.txt';
unlink($file);
#open the file (this line just creates the file to write)
$file = fopen("test.txt","w");
fclose($file);
#increase the counter by a number (10 for testing)
$temp = (int)$temp + 10;
$file=fopen("test.txt","w");
fwrite($file,"$temp \n")
fclose($file);
#the file only contains a number ($temp - the final counter)
echo "<h1>$temp<h1>"; #this outputs the correct counter in terminal but only outputs 10 on html page. ?????
?>
</body>
</html>
【问题讨论】:
-
你刷新了是吗?
-
是的,由于某种原因,它一直输出 10。虽然终端输出每次都是正确的。
-
那么你的写函数不工作了,var_dump fwrite 函数看看它是否工作。创建新文件时可能是权限问题,您可能必须将其更改为 777
-
当您从命令行运行 PHP 脚本时,它能够以您通常的权限访问文件系统。但是 Web 服务器是不同的用户(通常是“www”或“apache”)。这意味着它无权更改属于您的文件或文件夹。在
$file = fopen("test.txt","w");行之后检查$file的值——它可能是错误的。并检查您的错误日志。 -
嗯,我试过用 766 做,应该可以。如何检查 $file 的值?