Introduction to common file operations and read/write functions in php

Source: Internet
Author: User
This article introduces the following common file operation functions: file_get_contents, which reads the entire file content.

This article introduces the following common file operation functions: file_get_contents, which reads the entire file content, fopen, creates and opens the file, fclose, closes the file, fgets, reads the file content, file_exists, and checks whether the file or directory contains file_put_contents write files

Use the PHP built-in function file_exists to check whether a file or directory exists. if the file or directory exists, the file_exists function returns TRUE. if the file or directory does not exist, FALSE is returned.

The following is a simple example code to check whether a file exists:

The instance code is as follows:

  1.  
  2.  
  3. ?
  4. $ Filename = "C: \ blabla \ php \ hello.txt ";
  5. If (file_exists ($ filename ))
  6. {Echo "The file $ filename exists .";
  7. } Else {
  8. Echo "The file $ filename does not exist ."
  9. ;}?>
  10.  
  11.  

If the file exists, the result of executing the PHP file is:

The file C: blablaphphello.txt exists.

If the file does not exist, the result of executing the PHP file is:

The file C: blablaphphello.txt does not exist.

You can also use the file_exists function to test whether a directory exists. the sample code is as follows:

The instance code is as follows:

  1. If (file_exists ("C: \ blabla \ php "))
  2. {Echo "yes ";}
  3. Else
  4. {Echo "no ";}

You can use the PHP built-in function file_get_contents to read the entire file content.

The file_get_contents function reads the entire file and returns a string. file_get_contents. the simplest syntax is as follows:

File_get_contents (filepath)

For example, you have a. txt file whose path is:

C: blablaphphello.txt

The following php code reads the file using the file_get_contents function and outputs the file content:

The instance code is as follows:

  1.  
  2.  
  3. $ F = file_get_contents ("C: \ blabla \ php \ hello.txt ");
  4. Echo $ f;?>
  5.  
  6.  

Note: Because the file path contains a backslash, in the PHP string, the backslash needs to be escaped, which must be expressed by two backslashes. (If you forget to escape some special characters in PHP, read the PHP strings mentioned above .)

The return value of the file_get_contents function is the string of the read file content. If an error occurs, FALSE is returned.

Use the PHP built-in function fopen to open a file.

Open a file

The simplest syntax of fopen is as follows:

Fopen (filepath, mode)

The following is an example of PHP code for opening a file:

The instance code is as follows:

  1. $ F = fopen ("c: \ data \ info.txt", "r ");
  2. ?>

C: \ data \ info.txt indicates the file path, and r indicates that the mode for opening the file is read-only.

The fopen function can open files in the following modes:

Mode description

R read-only. the file pointer is at the beginning of the file.

R + read and write. the file pointer is at the beginning of the file.

W write only, the file pointer is at the beginning of the file, cut the file length to 0.

If the file does not exist, create the file.

W + read and write. the file pointer starts at the beginning of the file and cut the file length to 0.

If the file does not exist, create the file.

A writes only, and the file pointer is at the end of the file.

If the file does not exist, create the file.

A + read and write. the file pointer is at the end of the file.

If the file does not exist, create the file.

X writes only, and the file pointer is at the beginning of the file.

If the file already exists, the fopen () function returns FALSE and generates an E_WARNING-level error.

If the file does not exist, create the file.

X + reads and writes. the file pointer is at the beginning of the file.

If the file already exists, the fopen () function returns FALSE and generates an E_WARNING-level error.

If the file does not exist, create the file.

If the file is successfully opened, the fopen function returns a file pointer resource (a file pointer resource). If an error occurs, FALSE is returned.

Create a file

Select the appropriate value of the fopen function parameter mode. you can use fopen to create a file, for example:

The instance code is as follows:

  1. $ F = fopen ("c: \ data \ 101.txt"," w ");
  2. $ F = fopen ("c: \ data \ 102.txt"," w + ");
  3. $ F = fopen ("c: \ data \ 103.txt"," ");
  4. $ F = fopen ("c: \ data \ 104.txt"," a + ");
  5. $ F = fopen ("c: \ data \ 105.txt"," x ");
  6. $ F = fopen ("c: \ data \ 106.txt"," x + ");
  7. ?>

The PHP built-in function fgets can be used to read a row of content in a file.

The syntax for fgets to read a row of content is:

Fgets (filepointer)

The following example shows how to read a file in one row.

Suppose we have a sites.txt file, which has three lines and the content is as follows:

Woyouxian.comblabla.cngoogle.com

The file path of sites.txt is:

C: blablaphpsites.txt

We use a PHP line to read the file content. The PHP code is as follows:

The instance code is as follows:

  1.  
  2.  
  3. $ F = fopen ("C: \ blabla \ php \ sites.txt", "r ");
  4. While (! Feof ($ f )){
  5. $ Line = fgets ($ f );
  6. Echo "site:", $ line ," ";
  7. }
  8. Fclose ($ f);?>
  9.  
  10.  

Run the PHP file and the returned result is:

Site: woyouxian. comsite: blabla. cnsite: google.com

The first line of the PHP code is to open the file, and the last line is to close a file. the while loop statement in indicates that when the file is not finished, a row is read and executed cyclically until the file pointer ends at the end of the article.

The feof function is a built-in function of PHP to test whether the file pointer has reached the end of the file. TRUE is returned. if not, FALSE is returned. the eof is end of file, which is easy to remember.

Normally, the return value of the fgets function is a string. If an error occurs, FALSE is returned.

Describes how to use the PHP built-in function fclose to close a file.

The syntax of the fclose function is as follows:

Fclose (filepointer)

If the operation succeeds, the fclose function returns TRUE. if the operation fails, the fclose function returns FALSE.

The following is a PHP code example of a fclose function:

The instance code is as follows:

  1. $ F = fopen ("c: \ data \ info.txt", "r ");
  2. Fclose ($ f );
  3. ?>

This section describes how to use fopen, fwrite, and fclose to open files, write files, and save closed files. it focuses on the fwrite function.

PHP built-in function fwrite is used to write files.

The common syntax of the fwrite function is:

Fwrite (handle, string)

The handle parameter indicates the file pointer resource (usually created by the fopen function) and the string parameter indicates the content to be written.

The following PHP code example demonstrates how to create a new file, write the content, and save and close the file:

The instance code is as follows:

  1. $ F = fopen ("C: \ blabla \ php \ write.txt", "w ");
  2. Fwrite ($ f, "It is awesome."); fclose ($ f); echo "done ";
  3. ?>
  4.  
  5.  

After the PHP file is executed, a file with the path C: blablaphpwrite.txt will be created. the file content is It is awesome ..

If you want to append content to an existing file, you only need to modify the mode value of fopen, as shown below:

$ F = fopen ("C: \ blabla \ php \ write.txt", "");

For the mode value of the fopen function, see fopen.

The fwrite function returns the number of bytes written to the file. If an error occurs, FALSE is returned.

PHP built-in function file_put_contents is used to write files.

The file_put_contents function is the simplest method. it can use only two parameters, one is the file path and the other is the content to be written. The syntax is as follows:

File_put_contents (filepath, data)

If the file does not exist, the file_put_contents function automatically creates the file. if the file already exists, the original file is overwritten.

You can use the file_put_contents function to create and write a new file, or rewrite an original file.

The following is a PHP code example using the file_put_contents function:

The instance code is as follows:

  1.  
  2. $ Content = "one for all ";
  3. File_put_contents ($ path, $ content );
  4. If (file_exists ($ path ))
  5. {Echo "OK" ;}else {echo "ng ";}
  6. ?> 

The PHP code example will create a file named C: blablafilesysone.txt with the content of one for all.

If you want to append content to an existing file, you can also use the file_put_contents function, just add a parameter.

File_put_contents (filepath, data, flags)

When the flags value is FILE_APPEND, it indicates that the content is appended to an existing file.

For example, if we want to append content to the C: blablafilesysone.txt file in the preceding example, we can write as follows:

The instance code is as follows:

  1. $ Path = "C: \ blabla \ filesys \ one.txt ";
  2. $ Content = "all for one ";
  3. File_put_contents ($ path, $ content, FILE_APPEND );
  4. If (file_exists ($ path ))
  5. {Echo "OK" ;}else {echo "ng ";}
  6. ?> 
  7.  

After executing the PHP file, let's look at the C: blablafilesysone.txt file again and find that the file content has increased:

One for all for one

The file_put_contents function returns the number of bytes written to the file (number of bytes). If an error occurs, FALSE is returned.

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.