PHP中fwrite与file_put_contents的区别

来自AI助手的总结
file_put_contents可追加写入,fwrite会覆盖原内容。

相同点: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);

file_put_contents()函数详解

fwrtie()函数详解

温馨提示: 本文最后更新于2025-03-02 00:06:03,某些文章具有时效性,若有错误或已失效,请在下方 留言或联系 蚂蚁官方
© 版权声明
THE END
喜欢就支持一下吧
点赞12赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容