Home >Backend Development >PHP Tutorial >Instructions for using the php file_get_contents() function and file_put_contents() function

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

怪我咯
怪我咯Original
2017-07-11 11:03:242297browse

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)
Parameters Description
path Required. Specify the file or url to be read.
include_path Optional. Set this parameter to '1' if you also want to search for files in include_path (in php.ini).
context Optional. 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.
start Optional. Specifies the position in the file to begin reading. This parameter is new in PHP 5.1.
max_length Optional. 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)
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!

Statement:
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