首頁  >  文章  >  後端開發  >  PHP中fwrite與file_put_contents的差別

PHP中fwrite與file_put_contents的差別

高洛峰
高洛峰原創
2017-06-05 11:28:121779瀏覽

相同點:file_put_contents() 函數把字串寫入檔案中,與依序呼叫 fopen(),fwrite() 以及 fclose() 函數一樣。

不同點:在file_put_contents()函數中使用 FILE_APPEND 可避免刪除檔案中已有的內容,即實作多次寫入同一個檔案時的追加功能。

例如: 

echo file_put_contents("test.txt","Hello World. Testing!",FILE_APPEND);

file_put_contents是以追加的形式將字串寫入到test.txt中,

fwrtie是會清除之前的記錄,只保留當前寫入的內容

$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn