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

Source: Internet
Author: User
Tags php code

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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.