Php file processing ~~ Learning notes _ PHP Tutorial

Source: Internet
Author: User
Php file processing ~~ Study notes. Php file processing ~~ The fopen function is used to open a file in PHP. The first parameter of this function contains the name of the file to be opened. The second parameter specifies the mode used to open the php file for processing ~~ Study Notes
The fopen function is used to open a file in PHP.
The first parameter of this function contains the name of the file to be opened, and the second parameter specifies the mode used to open the file;

The file may be opened in the following mode:
Bytes -------------------------------------------------------------------------------------------
Mode | description
Bytes -------------------------------------------------------------------------------------------
R | read-only. Start at the beginning of the file.
Bytes -------------------------------------------------------------------------------------------
R + | read/write. Start at the beginning of the file.
Bytes -------------------------------------------------------------------------------------------
W | write only. Open and clear the file content. if the file does not exist, create a new file.
Bytes -------------------------------------------------------------------------------------------
W + | read/write. Open and clear the file content. if the file does not exist, create a new file.
Bytes -------------------------------------------------------------------------------------------
A | append. Open and write to the end of the file. if the file does not exist, create a new file.
------------------------------------------------------------------------------------------
A + | read/append. Write content to the end of the file to keep the file content.
Bytes -------------------------------------------------------------------------------------------
X | write-only. Create a new file. If the file already exists, FALSE is returned.
Bytes -------------------------------------------------------------------------------------------
X + | read/write. Create a new file. If the file already exists, FALSE and an error are returned.
Bytes -------------------------------------------------------------------------------------------

Note: If fopen () cannot open the specified file, 0 (false) is returned ).

Bytes -------------------------------------------------------------------------------------------

If fopen () cannot open the specified file, the following example generates a message:


$ File = fopen ("lala.txt", "r") or exit ("unable to open the file ");
?>
Bytes -------------------------------------------------------------------------------------------
Close the file:

The fclose () function is used to close open files.


$ File = fopen ("test.txt", "r ");

// Some code to be executed

Fclose ($ file );
?>
Bytes -------------------------------------------------------------------------------------------
Detect End-of-file
The feof () function checks whether it has reached the end of the file (EOF ).
The feof () function is useful when data with unknown length is traversed cyclically.

Note: in w, a, and x modes, you cannot read open files!
Bytes -------------------------------------------------------------------------------------------
Read files row by row
The fgets () function is used to read objects row by row from objects.

Note: after the function is called, the file pointer is moved to the next row.


$ File = fopen ("Kecily.txt", "r") or exit ("unable to open the file ");
While (! Feof ($ file )){
Echo fgets ($ file )."
";
}
Fclose ($ file );
?>
Bytes -------------------------------------------------------------------------------------------
Read files by character
The fgetc () function is used to read files from a file character by character.

Note: after the function is called, the file pointer is moved to the next character.


$ File = fopen ("Kecily.txt", "r") or exit ("unable to open the file ");
While (! Feof ($ file )){
Echo fgetc ($ file ).;
}
Echo"
";
Fclose ($ file );

?>


Region ~~ The fopen function is used to open a file in PHP. The first parameter of this function contains the name of the file to be opened. The second parameter specifies the mode used to open the file...

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.