PHP common file operation read-write function Introduction _php tutorial

Source: Internet
Author: User
Tags truncated
This article describes the following several commonly used file operation functions file_get_contents read the entire file contents fopen Create and open files Fclose close the file fgets read the file one line of content file_exists check whether the file or directory exists File_put_ Contents Write file fwrite write file

Use the PHP built-in function file_exists to check if a file or directory exists. If the file or directory exists, the File_exists function returns TRUE and returns FALSE if it does not exist.

The following is an example code that checks whether a file exists:

The code is as follows Copy Code
$filename = "C:\blabla\php\hello.txt";
if (file_exists ($filename))
{echo "the file $filename exists.";
}else {
echo "The file $filename does not exist."
;}? >


If the file exists, the execution of the PHP file displays the following results:

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, as shown in the sample code:

The code is as follows Copy Code

if (file_exists ("C:\blabla\php"))
{echo "yes";}
Else
{echo "No";}


With PHP built-in function file_get_contents can read the entire file content.

The File_get_contents function reads the entire file, returning a string. The simplest wording of file_get_contents is as follows:

File_get_contents (filepath)
For example, you have a. txt file with the path to the file:

C:blablaphphello.txt
The following PHP code reads the file with the File_get_contents function and outputs the contents of the file:

The code is as follows Copy Code


$f = file_get_contents ("C:\blabla\php\hello.txt");
Echo $f;? >


Note: Because the file path contains a backslash, the backslash needs to be escaped in the PHP string, which is represented by two backslashes. (If you forget the escape of some special characters in PHP, please read the previous PHP string.) )

The return value of the File_get_contents function is the read file content string, which returns FALSE if an error occurs.


With PHP built-in function fopen, you can open a file.

Open File
The simplest syntax for fopen is as follows:

fopen (Filepath,mode)
Here is an example of a PHP code that opens a file:

The code is as follows Copy Code
$f = fopen ("C:\data\info.txt", "R");
?>

Where C:\data\info.txt is the file path, r indicates that the mode of open file (mode) is read only (read only) mode.

The fopen function opens a file in the following modes:

Mode description
R is read-only and the file pointer is at the beginning of the file.
r+ Read and write, the file pointer at the beginning of the file.
W write only, the file pointer at the beginning of the file, the length of the file truncated to 0.

If the file does not exist, create the file.

w+ Read and write, the file pointer at the beginning of the file, the length of the file truncated 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, and 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 produces an e_warning level error.

If the file does not exist, create the file.

x+ Read and write, the file pointer at the beginning of the file.

If the file already exists, the fopen () function returns false and produces an e_warning level error.

If the file does not exist, create the file.

If the file is opened successfully, the return value of the fopen function is a file pointer resource (a files pointer resource) and returns FALSE if an error occurs.

Create a file
Select the appropriate value for the fopen function parameter mode, and you can create a file with fopen, such as:

The code is as follows Copy Code

$f = fopen ("C:\data\101.txt", "w");
$f = fopen ("C:\data\102.txt", "w+");
$f = fopen ("C:\data\103.txt", "a");
$f = fopen ("C:\data\104.txt", "A +");
$f = fopen ("C:\data\105.txt", "X");
$f = fopen ("C:\data\106.txt", "x+");
?>

With PHP built-in function fgets can read a line of the file.

Fgets The syntax for reading a file line is:

Fgets (Filepointer)
Let's give an example of how to read a file in a row.

Let's say we have a sites.txt file with three lines that reads as follows:

Woyouxian.comblabla.cngoogle.com
The file path for the Sites.txt is:

C:blablaphpsites.txt
We use PHP line to read the contents of the file, the PHP code is as follows:

The code is as follows Copy Code


$f = fopen ("C:\blabla\php\sites.txt", "R");
while (!feof ($f)) {
$line = fgets ($f);
echo "site:", $line, "
";
}
Fclose ($f);? >


Executing the PHP file, the returned display results are:

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 indicates that when a file is not finished, it reads a line and loops through it until the file pointer ends at the end of the article.

The feof function is a built-in function of PHP to test whether a file pointer has reached the end of the file. If it is true, returns FALSE if it is not. 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 and returns FALSE if an error occurs.


Tells you to close a file by using the PHP built-in function fclose.

The Fclose function syntax is as follows:

Fclose (Filepointer)
If successful, the Fclose function returns TRUE if the Fclose function returns FALSE if it fails.

Here is an example of a PHP code for the Fclose function:

The code is as follows Copy Code

$f = fopen ("C:\data\info.txt", "R");
Fclose ($f);
?>


In this chapter we describe how to use fopen, fwrite, fclose to implement this sequence of opening files, writing files, and saving closed files. Focus on the Fwrite function.

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

The common syntax for the fwrite function is:

Fwrite (handle,string)
Where the parameter handle represents the file pointer resource (typically created by the fopen function), and string represents the content to write.

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

The code is as follows Copy Code
$f = fopen ("C:\blabla\php\write.txt", "w");
Fwrite ($f, "It is awesome."); Fclose ($f); echo "Done";
?>


After executing the PHP file, a file with the path of C:blablaphpwrite.txt is created and the content of the file is the it is awesome.

If you want to append the content to the existing file, you just need to change the fopen parameter mode value, as follows:

$f = fopen ("C:\blabla\php\write.txt", "a");
For the fopen function, see fopen for the parameter mode value.

The Fwrite function returns the bytes written to the file (number of bytes), and returns FALSE if an error occurs.

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

The simplest way to write a file_put_contents function is to use only two parameters, one for the file path and one for the content to be written, with the following syntax:

File_put_contents (Filepath,data)
If the file does not exist, the File_put_contents function automatically creates the file, and if the file already exists, the original file is rewritten.

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

Here is an example of a PHP code that uses the File_put_contents function:

The code is as follows Copy Code

$content = "one for all";
File_put_contents ($path, $content);
if (file_exists ($path))
{echo "OK";} else {echo "ng";}
?>

The PHP code example creates a file with a path of c:blablafilesysone.txt, and the content of the file is 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 value of flags is file_append, it means that the content is appended to the existing file.

For example, we want to append content to the C:blablafilesysone.txt file on the example above, we can write:

The code is as follows Copy Code
$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";}
?>

After executing the PHP file, we looked at the C:blablafilesysone.txt file and found that the contents of the file increased and became:

One for all and for one
The File_put_contents function returns the bytes written to the file (number of bytes), and returns FALSE if an error occurs.

http://www.bkjia.com/PHPjc/629216.html www.bkjia.com true http://www.bkjia.com/PHPjc/629216.html techarticle This article describes the following several commonly used file operation functions file_get_contents read the entire file contents fopen Create and open files fclose close file fgets read a file line of content file_e ...

  • 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.