PHP fwrite 与 file_put_contents 的区别

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

<meta charset="UTF-8">
<?php
//file_put_contents是以追加的形式将字符串写入到test.txt中,
echo file_put_contents("test.txt","阅谁问君诵,水落清香浮。",FILE_APPEND);
?>

效果图:

 

<meta charset="UTF-8">
<?php
//fwrtie则是会清除之前的记录,只保留当前写入的内容
$file = fopen("test2.txt","w");
echo fwrite($file,"一直专注于Web,http://onestopweb.iteye.com/");
fclose($file);
?>

效果图:

 

猜你喜欢

转载自onestopweb.iteye.com/blog/2335819