Instructions for using the php file_get_contents() function and file_put_contents() function

怪我咯
Release: 2023-03-13 14:04:01
Original
2248 people have browsed it

file_get_contents — Read the entire file into a string

file_get_contents() Read the entire file into a string. This function is the preferred method for reading the contents of a file into a string. If supported by the server operating system, memory mapping technology is also used to enhance performance.

Similar functions: fopen() function opens a file or URL. The difference is that after fopen() opens the web page, it does not return a string and cannot be output directly. You also need to use the fgets() function to obtain the string. The fgets() function reads a line from the file pointer. The file pointer must be valid and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).

Syntax

file_get_contents(path,include_path,context,start,max_length)
Copy after login
ParametersDescription
path Required. Specify the file or url to be read.
include_pathOptional. Set this parameter to '1' if you also want to search for files in include_path (in php.ini).
contextOptional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream. If NULL is used, it is ignored.
startOptional. Specifies the position in the file to begin reading. This parameter is new in PHP 5.1.
max_lengthOptional. Specifies the number of bytes to read. This parameter is new in PHP 5.1.

The file_put_contents() function writes a string to a file.

When this function accesses a file, it follows the following rules:

  • If FILE_USE_INCLUDE_PATH is set, the built-in path for the copy of *filename* will be checked

  • If the file does not exist, a file will be created

  • Open the file

  • If LOCK_EX is set, then Locking the file

  • If FILE_APPEND is set, it will be moved to the end of the file. Otherwise, the contents of the file will be cleared

  • Write data to the file

  • Close the file and unlock all files

If successful, this function returns the number of characters written to the file. On failure, False is returned.

 file_put_contents(file,data,mode,context)
Copy after login
contextOptional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream.
Parameters Description
file Required. Specifies the file to which data is to be written. If the file does not exist, a new file is created.
data Required. Specifies the data to be written to the file. Can be a string, array, or data stream.
mode Optional. Specifies how to open/write the file. Possible values:
  • FILE_USE_INCLUDE_PATH

  • FILE_APPEND

  • ##LOCK_EX

PS: Please use FILE_APPEND to avoid deleting existing content in the file.

The above is the detailed content of Instructions for using the php file_get_contents() function and file_put_contents() function. For more information, please follow other related articles on the PHP Chinese website!

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