Define and use the fopen () function to open a file or URL. If opening fails, this function returns FALSE. The fopen (filename, mode, include_path, context) parameter description is required for filename. Specifies the file or URL to open. Mode is required. Specifies the access type of the file stream. Possible values are shown in the table below. Include _
Define and use the fopen () function to open a file or URL. If opening fails, this function returns FALSE. The fopen (filename, mode, include_path, context) parameter description is required for filename. Specifies the file or URL to open. Mode is required. Specifies the access type of the file/stream. Possible values are shown in the table below. Include _
Definition and usage
The fopen () function opens a file or URL.
If opening fails, this function returns FALSE.
Syntax
fopen(filename,mode,include_path,context)
| Parameters |
Description |
| Filename |
Required. Specifies the file or URL to open. |
| Mode |
Required. Specifies the access type of the file/stream. Possible values are shown in the table below. |
| Include_path |
Optional. If you also need to retrieve the file in include_path, you can set this parameter to 1 or TRUE. |
| Context |
Optional. Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream. |
Possible value of the mode Parameter
| Mode |
Description |
| "R" |
Open the file in read-only mode and point the file pointer to the file header. |
| "R +" |
Open in read/write mode and point the file pointer to the file header. |
| "W" |
Open in writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. |
| "W +" |
Open in read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. |
| "" |
Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it. |
| "A +" |
Open in read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it. |
| "X" |
Create and open the file in writing mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it.
This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) System Call.
This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |
| "X +" |
Create and open the file in read/write mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it.
This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) System Call.
This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |
Definition and usage
The fwrite () function writes data to a file (which can be safely used for binary files ).
Syntax
fwrite(file,string,length)
| Parameters |
Description |
| File |
Required. Specifies the open file to be written. |
| String |
Required. Specifies the string of the file to be written. |
| Length |
Optional. Specifies the maximum number of bytes to write. |
Description
Fwrite ()StringContent writing pointerFile. IfLengthWhenLengthBytes or have been writtenStringLater, the write operation will stop, depending on the first situation.
Fwrite () returns the number of written characters. If an error occurs, false is returned.
Note:
# Chmod 0666 log.txt
Output 21 indicates that the write is successful.