PHP file processing

Source: Internet
Author: User
: This article mainly introduces PHP file processing. if you are interested in PHP tutorials, please refer to it. 1. open a file

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

// Use "/" for the path separator of the file name

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

Mode Description
R Read-only. Start at the beginning of the file.
R + Read/write. Start at the beginning of the file.
W Write-only. Open and clear the file content. if the file does not exist, create a new file.
W + Read/write. Open and clear the file content. if the file does not exist, create a new file.
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.
X Write-only. Create a new file. If the object already exists, FALSE is returned.

X +

Read/write. Create a new file. If the file already exists, FALSE and an error are returned.

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

2. read file content

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

string fgetc( resource handle)
// Returns a character. in case of EOF, FALSE is returned.

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

string fgets( int handle [, int length])

// Length: the length of the data to be read.

The fgets () function can read a row from the specified handle file and return a string with a maximum length of-1 bytes. When a line break, EOF, or the length-1 byte is read, it is stopped. if the length parameter is ignored, the row ends. Note: after the function is called, the file pointer is moved to the next row.

The fgetss () function reads a row from an opened file and filters out HTML and PHP tags.

(3) read a string of any length: fread () read open files. You can also read binary files.

string fread( int handle, int length)

// Length specifies the number of bytes to read. This function stops execution when it reads length bytes or reaches EOF.

Filesize (handle) gets the length of the file data.

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

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

// Readfile () reads a file and writes it to the output buffer. the number of bytes to be read is returned successfully. if it fails, FALSE is returned.


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

// File () reads the content of the entire file into an array. An array is returned. each element in the array is a line corresponding to the file, including a line break. if the element fails, FALSE is returned.

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

// File_get_contents () reads the object content into a string. If the offset and maxlen parameters exist, the content with the length of maxlen is read at the position specified by the offset parameter. Otherwise, FALSE is returned.
Apply the readfile (), file (), and file_get_contents () functions. you do not need to open or close the file, but do not need to output statements. simply apply the function.

However, you must use the fopen () function to open the file before reading a single character, one line of character, and any length of string. after reading the string, 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])

The fwrite () function writes the string content to the handle of the file pointer. The number of written characters is returned. if the write fails, FALSE is returned.

When the fwrite () function is applied, if the length parameter is given, the magic_quotes_runtime (options in the php. ini file) configuration option will be ignored, and the diagonal lines in the string will not be extracted. If this function is applied to systems that distinguish binary files from text files (such as Windows), 'B' is added to the mode parameter of the fopen () function when opening files '.

(2) file_put_contents (): fopen () and fclose () are not required ()

Write a string data to a file. the number of written bytes is returned successfully. if the string data fails, FALSE is returned.

int file_put_contents( string filename, string data [, int flags [, resource context]])
This function can be safely used for binary objects. If "fopen wrappers" has been activated, you can use the URL as the file name in this function.

4. close the file pointer.

bool fclose( resource handle)

Zookeeper

The above introduces PHP file processing, including some content, and hopes to help friends who are interested in PHP tutorials.

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.