Php file operations-read and write files

Source: Internet
Author: User
Php file operations-read and write files

  1. @ $ Fp = fopen ("http://bbs.it-home.org/abc123", 'w ');
  2. If (! $ Fp)
  3. {
  4. Echo 'file does not exist ';
  5. Exit;
  6. }
  7. ?>

The @ symbol indicates that PHP will suppress all errors generated by the current function call.

2. PHP needs to read the file after opening the file in PHP. the fgets () function is generally used.

This function can read a row of content from the file each time, and it constantly reads data, the value to the line break or the end symbol EOF the full text.

The fgets () function can only read one row of data. to read all the data of a file, use a circular statement. For example:

  1. While (! Feof ($ fp ))
  2. {
  3. $ Bruce = fgets ($ fp );
  4. Echo $ bruce;
  5. }

Here, the feof () function is used to check whether the file ends. The unique parameter of this function is the file pointer (that is, the file opened by $ fp ). Of course, you can also use the readfile () function to read the entire file at a time in PHP. This function includes opening a file, reading a file, outputting it to a browser, and closing the file. For example:

  1. $ Bruce = readfile ("http://bbs.it-home.org ");
  2. Echo $ bruce;
  3. ?>

3. close the file in PHP.

Use the fclose () function to close the file.

2. how to write data to files in PHP

Like PHP reading files, PHP writing files also requires opening files, writing data, and closing files. The above describes how to open and close a file. what is the data written to a file in PHP.

Use the fwrite () function, such as fwrite (file path, write content ):

  1. $ Bruce = fopen ("http://bbs.it-home.org/", "r ");
  2. If (! $ Bruce)
  3. {
  4. Echo 'file does not exist ';
  5. Exit;
  6. }
  7. While (! Feof ($ bruce ))
  8. {
  9. $ Rose = fgets ($ bruce );
  10. $ James = fopen ("index.htm", "");
  11. Fwrite ($ james, $ rose );
  12. Fclose ($ james );
  13. }
  14. Fclose ($ bruce );
  15. ?>
  16. The jbxue.com contents are generated as local files.

Other useful file functions: file_exists (): Check whether the file exists, return the boolean value filesize (): view the file size, you can directly echo the output unlink (): delete the file, note that PHP does not have the delete function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.