Home  >  Article  >  Backend Development  >  PHP file programming (3)-two ways to write files

PHP file programming (3)-two ways to write files

WBOY
WBOYOriginal
2016-07-25 08:59:24932browse
  1. /**

  2. * php file programming to write files
  3. * edit bbs.it-home.org
  4. */
  5. //Write file
  6. $file_path="text.txt";
  7. if(!file_exists($file_path)){
  8. echo "The file does not exist";
  9. exit();
  10. }
  11. //"a+" Append "w+" to the end of the file and rewrite

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

  13. $con="rnHello";
  14. for($i=0;$i<10;$i++){
  15. fwrite($fp,$con);}

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

Copy the code

Method 2, write the file through the file_put_contents function

  1. //Second way to write files
  2. $file_path="text.txt";
  3. $content="hello,worldrn";

  4. //The default for writing a string to a file is [FILE_USE_INCLUDE_PATH] "w+" re-write

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

  6. echo "OK" ;

  7. ?>

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