File system developed in php

Source: Internet
Author: User
File system developed in php 1. open/close the file
Use the fopen () function and fclose () function to open/close the file respectively. When opening a file, you should pay special attention to the possibility of deleting all the file content accidentally.
(1) open a file
The syntax format of the fopen () function is as follows:
Resource fopen (string filename, string mode [, bool use_include_path]);
The filename parameter is the name of the file containing the path to be opened. it can be a relative or absolute path. The mode parameter is the open mode, as shown in the following table:

(2) close the file
Close the file after the file operation is completed. Otherwise, an error may occur. Use the fclose () function in php to close the file. The syntax is as follows:
Bool fclose (resource handle );
This function will close the handle object. if it succeeds, true is returned. otherwise, false is returned. The file pointer must be valid and be a file successfully opened through the fopen () function.
The instance code is as follows:

 

2. read and write files
To read data from a file, you can read one character, one line of string, or the entire file. It can also be a string of any length.
(1) Read the entire file: readfile (), file () and file_get_contents () functions.
A, readfile () function
The readfile () function is used to read a file and write it to the output buffer. If an error occurs, false is returned. The function syntax is as follows:
Int readfile (string filename );
Note:
Use readfile (0 function does not need to open/close the file, you only need to enter the file path. The echo/print output statement is not required.

B, file () function
The file () function can also read the data of the entire file, but the file () function stores the content of the file in the array by row. Including line breaks. If it fails, false is returned. The syntax format is as follows:
Array file (string filename );

C, file_get_contents () function
This function reads the file 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. If the Read fails, false is returned. The syntax format is as follows:
Sting file_get_contents (string filename [, int offset [, int maxlen]);
This function is applicable to binary objects. Is the preferred method to read the content of the entire file to a string.

The sample code is as follows:

 
Use the readfile () function to read the file content:
Use the file () function to read the file content:
Use the file_get_contents () function to read the file content:

The running result is as follows:

(2) read a row of data: fgets () and fgetss ()
The fgets () function is used to read a row of data at a time. the syntax is as follows:
String fgets (int handle [, int length]);
The handle parameter is an opened file, and the length parameter is the length of the data to be read. The function can read strings with a maximum length of 1 byte from handle. In case of a line break, the EOF or the length-1 byte is read and then stopped. If the length parameter is ignored, the data is read until the end of the row.

The fgetss () function is a variant of the fgets () function, used to read a row of data. At the same time, the fgets () function filters out the html and php tags in the read content.
The syntax format is as follows:
String fgetss (resource handle [, int length [, string allowable_tags])
This function can filter out any html and php tags from the file. You can use the allowable_tags parameter to control which tags are not filtered out.
The following is an example of using the fgets () and fgetss () functions. the code is as follows:

 
Use the fgets () function:
Use the fgetss () function:

The running result is as follows:

(3) fgetc () reads a character
When searching for a character, you need to read it in a targeted manner. The syntax format is as follows:
String fgetc (resource handle)
The sample code is as follows:

 "; } fclose($handle); ?>

123. The txt file is as follows:

The running result is as follows:

(4) fread () reads strings of any length
The syntax format is as follows:
String fread (resource [, int length])
The sample code is as follows:

 
 "; // Read string of 40 bytes. Fclose ($ handle);?>

123. The txt content is as follows:

The running result is as follows:

(5) write data to a file
Use the fwrite () and file_put_contents () functions in php to write data to a file.
The fwrite () syntax is as follows:
Int fwrite (resource handle, string [, int length])
This function writes the string to the handle. if the length is specified, it is written to the length byte and then stopped. If not, write all the string content.
The new function of php5 when file_put_contents () is used. Syntax:
Int file_put_contents (string filename, string data [, int flags])
The path name of the file whose filename is to be written.
Data is the data to be written.
Flags can be FILE_INCLUDE_PATH, FILE_APPEND or LOCK_X, and LOCK_EX is an exclusive lock.
The following is a comparison. Use the fopen (), fwrite (), fclose () and file_put_contents () functions.
The sample code is as follows:

 "; $ Fopen = fopen ($ filepath," wb ") or die (" file does not exist "); fwrite ($ fopen, $ str); fclose ($ fopen ); readfile ($ filepath); echo "writes files using the file_put_contents () function:
"; $ Str1 =" this is to use the file_put_contents () function to write 1111 "; file_put_contents ($ filepath, $ str1); readfile ($ filepath);?>

The running result is as follows:

2. file operations
The following table lists common file operation functions:

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.