PHP file processing

WBOY
Release: 2016-08-08 09:27:03
Original
1009 people have browsed it

1. Open the file

resource fopen( string filename, string mode [, int use_include_path [, resource zcontext]])
Copy after login

//The path separator of the file name is "/"

//mode: The file may be opened in the following modes:

Mode Description
r Read only. Start at the beginning of the file.
r+ read/write. Start at the beginning of the file.
w Just write. Opens and clears the contents of the file; if the file does not exist, creates a new file.
w+ read/write. Opens and clears the contents of the file; if the file does not exist, creates a new file.
a Added. Opens and writes to the end of a file, or creates a new file if it does not exist.
a+ read/append. Maintain file contents by writing to the end of the file.
x Just write. Create new file. Returns FALSE if the file exists.

x+

read/write. Create new file. If the file already exists, returns FALSE and an error.

Note: If fopen() cannot open the specified file, it returns 0 (false).

2. Read the file content

(1) Read a character: fgetc()Read a character from the position specified by the file pointer.

<span style="color:#000000;">string fgetc( resource handle)</span>
Copy after login
//Returns one character, returns FALSE if EOF is encountered.

(2) Read a line of characters: fgets()Read a line of data from the file pointer. The file pointer must be valid and must point to a file successfully opened by fopen() or fsockopen().

string fgets( int handle [, int length])
Copy after login

//length: The length of data to be read. The

fgets() function can read a line from the file specified by handle and return a string with a maximum length of length-1 bytes. Stops after encountering a newline character, EOF, or reading length-1 bytes. If the length parameter is omitted, the end of the line will be read. Note: After calling this function, the file pointer will move to the next line.

fgetss() function reads a line from the open file and filters out HTML and PHP tags.

(3)Read a string of any length: fread() reads the open file. Binary files can also be read.

string fread( int handle, int length)
Copy after login

//length specifies the number of bytes to read. This function stops execution when length bytes are read or EOF is reached.

filesize(handle) gets the length of file data.

(4) Read the entire file: readfile(), file() and file_get_contents()

int readfile( string filename [, bool use_include_path [, resource context]])
Copy after login

//readfile() reads a file and writes it to the output buffer, and returns the number of bytes read successfully , returns FALSE on failure.


array file( string filename [, int use_include_path [, resource context]])
Copy after login

//file() reads the contents of the entire file into an array. An array is returned successfully. Each element in the array is a corresponding line in the file, including newlines; FALSE is returned on failure.

string file_get_contents( string filename [, bool use_include_path [, resource context [,int offset [, int maxlen]]]])
Copy after login

//file_get_contents() reads the file contents into a string. If there are offset and maxlen parameters, content with a length of maxlen will be read starting from the position specified by the offset parameter. Returns FALSE on failure.
Apply the readfile(), file(), file_get_contents() function, there is no need to open/close the file, no need to output statements, just apply the function directly.

But when reading a character, a line of characters and a string of any length, you must use the fopen() function to open the file before reading. After the reading is completed, you must use the fclose() function to close the file.

3. Write data to the file

(1)fwrite(), alias fputs()

int fwrite( resource handle, string string [,int length])
Copy after login

fwrite() function writes the content of string to the file pointer handle. Returns the number of characters written on success, FALSE on failure.

When applying the fwrite() function, if the length parameter is given, the magic_quotes_runtime (option in the php.ini file) configuration option will be ignored, and the slashes in the string will not be removed. If you apply this function on a system that distinguishes binary files from text files (such as Windows), add 'b' to the mode parameter of the fopen() function when opening the file.

(2)file_put_contents(): No need to use fopen() and fclose()

Write a string data into the file, return the number of bytes written successfully, and return FALSE on failure.

int file_put_contents( string filename, string data [, int flags [, resource context]])
Copy after login
This function is safe to use on binary objects. If "fopen wrappers" has been activated, the URL can be used as the file name in this function.

4. Close the file pointer

bool fclose( resource handle)
Copy after login



The above has introduced file processing in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template