[英]php file read and write
因此,即時通訊學習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>
我認為這是許可問題。 例如,嘗試更改chmod / chown:
sudo chown coders:www-data test.txt
sudo chown 765 test.txt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.