How to write to a file using PHP's fwrite() function

不言
Release: 2023-04-05 11:40:01
Original
4707 people have browsed it

The fwrite() function in How to write to a file using PHPs fwrite() function is used to write data to files. In this article, we will take a look at how PHP's fwrite() function writes files.

How to write to a file using PHPs fwrite() function

The basic syntax of the fwrite() function is as follows:

 fwrite(file, string, length)
Copy after login

1. file specifies the file name to be written
2. String specifies the string to be written to the file
3. Lenght specifies the maximum number of bytes to be written to the file (optional)

This function returns the total number of bytes written to the file, or Returns false on failure.

The following is an example of writing data to a file using How to write to a file using PHPs fwrite() function.

In the example below, the first line will open myfile.txt in edit mode in the current directory. The second line writes the string to the file, where you can also specify the length and generate the number of bytes written. On failure, false will be returned. The third line will close the open file.

The code is as follows:

<?How to write to a file using PHPs fwrite() function
  $fn = fopen("myfile.txt","w");
  $result = fwrite($fn,"Welcome to PHP!");
  fclose($fn);
?>
Copy after login

This article has ended here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to write to a file using PHP's fwrite() 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