2013-11-15 46 views
0

所以即時學習PHP和試圖使一個PHP的網頁點擊計數器。現在我已經根據我的理解編寫了這些代碼。問題是,當我在終端上運行程序時,我得到了正確的答案(輸出如我所料)。當我在html頁面上進行回顯時,它不起作用。這裏是代碼php文件讀取和寫入

<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> 
+0

你刷新了嗎? –

+0

是的,它出於某種原因始終輸出10。雖然終端輸出每次都能正確輸出。 – user2055171

+0

你的寫函數不工作,var_dump fwrite函數來找出它的工作與否。可能是創建新文件時的權限問題,您可能需要將其修改爲777 –

回答

0

我認爲這是權限問題。 嘗試改變chmod/chown爲例:

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