php中PHP fwrite与file_put_contents的区别

  相同点:file_put_contents() 函数把一个字符串写入文件中,与依次调用 fopen(),fwrite() 以及 fclose() 功能一样。
  不同点:在file_put_contents()函数中使用 FILE_APPEND 可避免删除文件中已有的内容,即实现多次写入同一个文件时的追加功能。

例如: 

 

 1 <?php

2 
3 echo file_put_contents("test.txt","Hello World. Testing!",FILE_APPEND);
4 
5 ?>

 

是以追加的形式将字符串写入到test.txt中,
而是用fwrtie则会清除之前的记录,只保留当前写入的内容

 

 1 <?php

2 $file = fopen("test.txt","w");
3 echo fwrite($file,"Hello World. Testing!");
4 fclose($file);
5 ?>

 

posted @ 2016-04-28 01:20  飞越全球  阅读(491)  评论(0编辑  收藏  举报