2017-01-03 96 views
1

使用的fopen時,我想讀&寫.txt文件中PHP什麼,我已經可以讀取使用不能使用「+」在PHP

$Readfile = fopen("file.txt", "r") or exit("Unable to open file!"); 

因爲我要覆蓋該文件的文件,我得到了建議使用

$Readfile = fopen("file.txt", "r+") or exit("Unable to open file!"); 

但總是最終得到「無法打開文件!」我也嘗試過使用其他像w+但仍然沒有給出任何結果。在這裏我的代碼

<?php 
     //$Readfile = fopen("pengumuman.txt","r") or exit("Unable to open file!"); 
     $Readfile = file_get_contents("pengumuman.txt"); 
     if (isset($_POST["btnSimpan"])) { 
      $pengumuman = $_POST["pengumuman"]; 
      fwrite($Readfile, $pengumuman); 
     } 
?> 

<textarea class="span12" style="min-height: 200px; height: auto;" name="pengumuman"> 
<?php 
    while(!feof($Readfile)) { 
    echo fgets($Readfile); 
    } 
?> 

一些更新我嘗試file_get_contents,但它給了我很長的加載和空白文本區域

回答

1

沒有錯的代碼,因爲我在Linux上運行它,我需要改變的權限。處理完畢後,所有事情都按照原樣運行。謝謝。如果這發生在你身上,只需按照這個鏈接php can't write to file in linux

0

而不是使用它,你可以嘗試:

$Readfile = file_get_contents("file.txt"); 

的過程由PHP處理得更好。後來,如果你想寫入文件,你可以使用file_put_contents這樣:

if (file_put_contents("file.txt", $Readfile . "Additional Content Here")) 
    echo "File written!"; 
else 
    echo "Error writing file!"; 
+0

我可以直接將它們保存爲.txt文件嗎? –

+0

@AndreasChandraGaozu是的,我已經給出了編寫文件的說明。檢查更新的答案? –

+0

@AndreasChandraGaozu你能用簡單的話來說,你想做什麼? –