php文件读写

时间:2013-11-15 00:44:44

标签: php html counter

所以即时通讯学习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>

1 个答案:

答案 0 :(得分:0)

我认为这是许可问题。 尝试更改chmod / chown,例如:

sudo chown coders:www-data test.txt
sudo chown 765 test.txt