| This article introduces the usage of the fwrite function in php for your reference. The PHP fwrite () function is used to write strings to a file. the number of written characters is returned. otherwise, FALSE is returned. Syntax: int fwrite (res
This article introduces the usage of the fwrite function in php for your reference.
The PHP fwrite () function is used to write strings to a file. the number of written characters is returned. otherwise, FALSE is returned.
Syntax: int fwrite (resource handle, string [, int length]) fwrite () writes string content to the file pointer handle. Parameter description: description of the file pointer of the string to be written in handle. Generally, the length of the string to be written by the fopen () function is optional and specifies the maximum number of bytes to be written.
If the optional parameter length is specified, the write will stop after the length bytes are written or the string is written.
Example: The code is as follows:
$ Fh = fopen ($ filename, "w"); echo fwrite ($ fh, $ word); // output: 6 fclose ($ fh);?>
Execute this sub-program. in the program directory, the content of the File.txt file is: Hello!
If the length parameter is used in the preceding example, at most length strings are written: echo fwrite ($ fh, $ word, 4); // output: 4
PHP fwrite append writes to the file, which is not related to the fwrite function, but to the mode in which the fopen function opens the file. When fopen opens a file, the mode parameter is set to a, indicating that the file is appended to the file: The code is as follows:
$ Fh = fopen ($ filename, "a"); echo fwrite ($ fh, $ word); fclose ($ fh);?>
PHP fwrite line feed write if you want to implement line feed write in the file, you only need to add line feed n to the place where line feed is needed in the write content: The code is as follows:
$ Fh = fopen ($ filename, "a"); echo fwrite ($ fh, $ word); fclose ($ fh);?>
In the above example, n linefeeds are added at the end of the content. To enable n to indicate a line break when writing a file, double quotation marks (for example, in the preceding example) must be used for writing. if single quotation marks are used, n characters are not interpreted as line breaks but are considered as n strings. Click to view details: the difference between single quotation marks and double quotation marks in a PHP string.
N is a line feed in the real sense. in windows, if you want to simulate a line break by carriage return (that is, when you open a file with a WordPad, it is not a black box but a line by line ), you can add the r carriage return before n: The code is as follows:
PHP fwrite write permission when opening an existing file (usually in append mode), it is necessary to check whether the file has write permission to avoid a system error. Use the is_writable function to check whether a file can be written.
The following is an example of a strict check in the append write mode: The code is as follows:
// Confirm that the file exists and can be written if (is_writable ($ filename) {// open the file if (! $ Fh = fopen ($ filename, 'A') {echo "cannot open the file $ filename"; exit;} // write the content if (fwrite ($ fh, $ word) === FALSE) {echo "cannot be written to the file $ filename"; exit;} echo "successfully writes $ word to the file $ filename"; fclose ($ fh );} else {echo "file $ filename cannot be written" ;}?>
The PHP fwrite function only writes strings to a file. the actual result depends on more fopen functions. To write or append a string to a file at a time, the file_put_contents function may be a better choice.
PHP file_put_contents function: writes or appends a string to a file. The PHP file_put_contents () function is the most suitable option for writing a string or appending a string content to a file at a time.
The file_put_contents () function is used to write a string to a file. the number of bytes written to the file is returned successfully, and FALSE is returned if the string fails.
Syntax: int file_put_contents (string filename, string data [, int flags [, resource context])
Parameter description: description of the data to be written by filename. The type can be string, array (but not multi-dimensional array), or stream resource flags (optional), specifying how to open/write files. Possible value: FILE_USE_INCLUDE_PATH: check the built-in path of the filename Copy. FILE_APPEND: write data to the end of the file as an append. LOCK_EX: locks the context of the file. optional. Context is a set of options, you can use it to modify text attributes.
Example: The code is as follows:
Run This example. the browser outputs: 18, and the content of the test.txt file (in the same directory as the program) is: This is something ..
If the file does not exist, a file is created, which is equivalent to the fopen () function. If the file exists, content in the file will be cleared by default. you can set the flags parameter value to FILE_APPEND to avoid this (see below ). This function can be safely used for binary objects.
When the flags parameter value is set to FILE_APPEND, new data is written after the existing file content: After the program is executed, the content of the test.txt file becomes: This is something. This is another something. file_put_contents (). The behavior is actually the same as calling the fopen (), fwrite (), and fclose () functions in turn.
Read more
PHP fopen: Create and open a file. PHP fwrite: write a string to a file. PHP close: close an open file. PHP file_get_contents: reads the entire file into a string. PHP checks whether the file can be read and written. execute the is_readable, is_writable, and is_executable functions.
The is_readable () function is used to check whether the file is readable. the is_writable () function is used to check whether the file can be written. the is_executable () function is used to check whether the file is executable. The is_readable () function is used to check whether the file is readable. if the specified file or directory exists and is readable, TRUE is returned; otherwise, FALSE is returned.
Syntax: bool is_readable (string filename) example: The code is as follows:
The is_writable () function is used to check whether the file can be written. if the file exists and can be written, TRUE is returned; otherwise, FALSE is returned.
Syntax: The bool is_writable (string filename) filename parameter can be a directory name that allows write check.
Example: The code is as follows:
The is_executable () function is used to check whether the file is executable. if the file exists and can be executed, TRUE is returned; otherwise, FALSE is returned. For usage, see is_writable (). |