PHP向文本文件累加数据

WBOY
Release: 2016-06-13 10:45:07
Original
1277 people have browsed it

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()已经包含锁了.
多余的操作!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!