Home > Backend Development > PHP Tutorial > php reads file content and clears file

php reads file content and clears file

WBOY
Release: 2016-07-25 08:53:44
Original
1883 people have browsed it
  1. //Read file content

  2. $fh = fopen($path, "r+");

  3. if( flock($ fh, lock_ex) ){//Add write lock

  4. $old_content=json_decode(fread($fh,filesize($path)),true);

  5. $old_content=$old_content.$new_content ;

  6. ftruncate($fh,0); // Truncate the file to the given length
  7. rewind($fh); // Rewind the position of the file pointer
  8. fwrite($fh,json_encode($old_content));
  9. / / @chmod($path,0644);
  10. flock($fh, lock_un); //Unlock

  11. }

  12. fclose($fh);

Copy Code


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