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, 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 a file or directory exists, the file_exists function returns TRUE. If not, FALSE is returned.

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

The Code is as follows: Copy code
<Html> <body> <? Php
$ Filename = "C: \ blabla \ php \ hello.txt ";
If (file_exists ($ filename ))
{Echo "The file $ filename exists .";
} Else {
Echo "The file $ filename does not exist ."
;}?>
</Body>
</Html>

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 Code is as follows: Copy code

If (file_exists ("C: \ blabla \ php "))
{Echo "yes ";}
Else
{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. The simplest Syntax of file_get_contents 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 Code is as follows: Copy code
<Html>
<Body>
<? Php
$ F = file_get_contents ("C: \ blabla \ php \ hello.txt ");
Echo $ f;?>
</Body>
</Html>

Note: because the file path contains a backslash, in the PHP string, the backslash must 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 Code is as follows: Copy code
<? Php
$ F = fopen ("c: \ data \ info.txt", "r ");
?>

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 the file length is cut 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 + reads and writes. 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. 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 Code is as follows: Copy code

<? Php
$ F = fopen ("c: \ data \ 101.txt"," w ");
$ F = fopen ("c: \ data \ 102.txt"," w + ");
$ F = fopen ("c: \ data \ 103.txt"," ");
$ F = fopen ("c: \ data \ 104.txt"," a + ");
$ F = fopen ("c: \ data \ 105.txt"," x ");
$ F = fopen ("c: \ data \ 106.txt"," x + ");
?>

You can use the PHP built-in function fgets 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 Code is as follows: Copy code
<Html>
<Body>
<? Php
$ F = fopen ("C: \ blabla \ php \ sites.txt", "r ");
While (! Feof ($ f )){
$ Line = fgets ($ f );
Echo "site:", $ line, "<br/> ";
}
Fclose ($ f);?>
</Body>
</Html>

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. If TRUE is returned, if not, FALSE is returned. The English meaning of 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 Code is as follows: Copy code

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


This section describes how to use fopen, fwrite, and fclose to open, write, and save closed files. Focus on fwrite functions.

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 Code is as follows: Copy code
<Html> <body> <? Php
$ F = fopen ("C: \ blabla \ php \ write.txt", "w ");
Fwrite ($ f, "It is awesome."); fclose ($ f); echo "done ";
?>
</Body>
</Html>

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 Code is as follows: Copy code
<Html> <body>
<? Php $ path = "C: \ blabla \ filesys \ one.txt ";
$ Content = "one for all ";
File_put_contents ($ path, $ content );
If (file_exists ($ path ))
{Echo "OK" ;}else {echo "ng ";}
?> </Body>

The PHP code example creates 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 Code is as follows: Copy code
<Html> <body> <? Php
$ Path = "C: \ blabla \ filesys \ one.txt ";
$ Content = "all for one ";
File_put_contents ($ path, $ content, FILE_APPEND );
If (file_exists ($ path ))
{Echo "OK" ;}else {echo "ng ";}
?> </Body>
</Html>

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.

Related Article

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.