2016-01-23 35 views
-1

我有一个文本文件number.txt,里面只有数字1。 我使用下面的代码打开文件,获取数字,添加1,更新内容(必须是2)并再次保存。我的代码不起作用。有什么建议么?打开txt文件抓住数字+1并再次保存

$fp = fopen('number.txt', 'c+'); 
flock($fp, LOCK_EX); 
$count = (int)fread($fp, filesize('number.txt')); 
ftruncate($fp, 0); 
fseek($fp, 0); 
fwrite($fp, $count + 1); 
flock($fp, LOCK_UN); 
fclose($fp); 
+0

它在哪里停止工作,做到这一点? –

回答

0

对我的作品

<?php 
    $handle = fopen("test.txt", "r+"); 
    if ($handle) { 
    $buffer = fgets($handle, 10); 
    $nCount = (int)$buffer; 
    rewind($handle); 
    fputs($handle, $nCount+1); 
    fclose($handle); 
    } 
?> 
0

你应该能够只

$path = 'number.txt'; 
file_put_contents($path, 1+(int)file_get_contents($path)); 
相关问题