Home  >  Article  >  Backend Development  >  php reads file content and clears file

php reads file content and clears file

WBOY
WBOYOriginal
2016-07-25 08:53:441829browse
  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


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