2014-05-11 62 views
-3

我的代碼生成該文件的一些problems.The以前的內容越來越overwrited如何寫一個PHP文件沒有得到它覆蓋

 <?php 
     $g=fopen("lop.txt","r"); 
     $m=fgets($g); 
     echo $m; 
     fclose($g); 
     $j="tendoeschate"; 
     $b=fopen("lop.txt","w"); 
     $p=fwrite($b,$j); 
     $t=fgets($b); 
     echo $t; 
     ?> 
在此我以前的內容

,當我追加新是越來越overwrited內容請幫忙

+2

因此,在附加模式('a')而不是簡單寫入('w')並閱讀[DOCS](http://www.php.net/manual/en/function.fopen.php)未來 –

+0

http://in1.php.net/manual/en/function.fopen.php – Vagabond

+0

檢查了這一點。 http://stackoverflow.com/questions/103593/using-php-how-to-insert-text-without-overwriting-to-the-beginning-of-a-text-fil – kjames

回答

1

通過更換FWRITE:

file_put_contents("lop.txt", $j, FILE_APPEND); 
0

您可以隨時抓住舊文件的內容,然後該字符串結合了新的內容字符串,然後寫完整的一行。

$existingData = file_get_contents($file); 
$newData = "Blablabla"; 
$totalData = $existingData.'. '.$newData; 

fwrite($file, $totalData); 

您還可以將$totalData左右,因此在舊數據的寫入前較新的數據。儘管如此,使用FILE_APPEND更有效。