当前位置:Gxlcms > PHP教程 > PHP文件编程(三)-写入文件的二种方式

PHP文件编程(三)-写入文件的二种方式

时间:2021-07-01 10:21:17 帮助过:23人阅读

  1. /**

  2. * php 文件编程 写入文件
  3. * edit bbs.it-home.org
  4. */
  5. //写文件
  6. $file_path="text.txt";
  7. if(!file_exists($file_path)){
  8. echo "文件不存在";
  9. exit();
  10. }
  11. //"a+" 在文件后面追加 "w+"重新写入

  12. $fp=fopen($file_path,"w+");

  13. $con="\r\n你好";
  14. for($i=0;$i<10;$i++){
  15. fwrite($fp,$con);}

  16. echo "添加成功";

  17. fclose($fp);
  18. ?>

方法2,通过file_put_contents函数写入文件

  1. //第二种方式写文件

  2. $file_path="text.txt";
  3. $content="hello,world\r\n";

  4. //将一个字符串写入文件 默认是【FILE_USE_INCLUDE_PATH 】"w+"重新写入

  5. file_put_contents($file_path,$content,FILE_APPEND);

  6. echo "OK";

  7. ?>

人气教程排行