Home >Backend Development >PHP Tutorial > PHP向文本文件累加数据

PHP向文本文件累加数据

WBOY
WBOYOriginal
2016-06-13 13:16:151490browse

PHP向文本文件追加数据
代码如下:  

//一分钟内 php文件 被访问5000次,每次都往文本文件里追加 $word 数据 $word= "ok!";

//请教各位高手,如果不锁定,追加的数据会出错吗,毕竟不是读,只是一个劲的追加数据

  $word= "ok!";

  $fn = 'a2.txt';
  $fp = fopen($fn, 'a');

  flock($fp, LOCK_EX); //锁定文件,避免读写

  fwrite($fp, $word);

  flock($fp, LOCK_UN); //解锁

  fclose($fp); //关闭程序流

如果不影响,那么我就不想锁定了. 期待.....
   



------解决方案--------------------
读的时候加锁(lock_sh),写的时候也进行加锁(lock_ex),这样程序才不会出现问题
------解决方案--------------------
file_put_contents( $fn, $word, LOCK_EX );
------解决方案--------------------
On versions of PHP before 5.3.2, the lock is released also by fclose() (which is also called automatically when script finished). 


fclose()已经包含锁了.
多余的操作!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn