File operations in php (9)

File operation The
common  function
file()
reads each line in the file into an array
parse_ini_file() is
used to parse a configuration file
Note : If the second parameter is set to true, a multidimensional array will be obtained. Include the name and settings of each category (section) in the configuration file.
file_get_contents()
reads the entire file into a string and
file_put_contents()
writes the string to the file. If the file does not exist, it will be automatically created
\ r and \n is the difference
\rEnter
tells the typewriter to position the print head on the left
\nLine feed
Tells the typewriter to move the paper down one line
Difference between systems
In linux system\nIn
windows, line feed is used\r\ In the n
mac system, each line is \r
the opening and closing of the file to
open the file and obtain the resource
fopen ('file position', 'mode')
returns the resource of a file
Mode
r
is opened in read-only mode, and the file pointer points to the head of the file , if the file does not exist, it will report an error
r +
read and write mode to open, the file pointer starts from the head, overwrite the write, and the file does not exist, it will report an error
w
Open in write mode, point the file pointer to the head of the file and truncate the size of the file to 0
w+
open in read-write mode, point the file pointer to the head of the file and truncate the size of the file to 0
a
open in write mode, and open the file in write mode Point to the end of the file (write the file by appending), if the file does not exist, create
a+
to open it in read-write mode, point the file to the end of the file, create it if the file does not exist, and read the
difference between w and r from the beginning It depends on whether the file does not exist or not.
Experience The
principle of least privilege
Operating file
fgets (resource)
reads one line at a time, reads a line, moves the pointer down
fgetc (resource)
reads one byte at a time, moves the pointer down
fread (resource, number of characters)
Read the specified number of characters
feof (resource)
file error or the pointer has pointed to the end of the file, return true
fwrite (resource, write content) write
the content to the file pointer
Close resource
fclose (resource)
Note that the
resource is open Even if these resources are closed without using the code, they will be automatically released after the code is all executed.
File locking mechanism
flock (resource, option)
option
LOCK_SH, when reading the file, others should not write content to it
LOCK_EX, when writing a file, other people cannot read and write the file
LOCK_UN, release the lock The
common  function
file()
reads each line in the file into an array
parse_ini_file() is
used to parse a configuration file
Note : If the second parameter is set to true, will get a multidimensional array. Include the name and settings of each category (section) in the configuration file.
file_get_contents()
reads the entire file into a string and
file_put_contents()
writes the string to the file. If the file does not exist, it will be automatically created
\ r and \n is the difference
\rEnter
tells the typewriter to position the print head on the left
\nLine feed
Tells the typewriter to move the paper down one line
Difference between systems
In linux system\nIn
windows, line feed is used\r\ In the n
mac system, each line is \r
the opening and closing of the file to
open the file and obtain the resource
fopen ('file position', 'mode')
returns the resource of a file
Mode
r
is opened in read-only mode, and the file pointer points to the head of the file , an error
r+ is reported if the file does not exist
Open in read-write mode, the file pointer starts from the head, overwrites the write, if the file does not exist, an error is reported
w
Open in write mode, point the file pointer to the head of the file and intercept the size of the file as 0
w+
open in read-write mode, set The file pointer points to the head of the file and truncates the size of the file to 0
a
to open it for writing, point the file to the end of the file (write to the file in appending mode), if the file does not exist, create
a+
to open it for reading and writing, put The file points to the end of the file. If the file does not exist, it will be created. The difference
between w and r is whether the file does not exist or not . The difference between w and r is whether the file does not exist . Move fgetc(resource) to read one byte at a time, move the pointer down fread(resource, chars) to read the specified number of characters feof(resource) Returns true when an error occurs in the file or the pointer has reached the end of the file fwrite(resource, write Enter the content) write the content to the file pointer Close resource fclose (resource) Note that the resource is opened and closed. Even if these resources are closed without code, they will be automatically released after the code is fully executed. File lock mechanism flock(resource, options)



















Option
LOCK_SH, when reading the file, other people do not write the content
LOCK_EX in it, when writing the file, others cannot read and write the file
LOCK_UN, release the lock

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644075&siteId=291194637
Recommended