Home > Article > Backend Development > How to write files in php
php method of writing files
PHP writing files-fwrite()
fwrite() function is used to write files.
The first parameter of fwrite() contains the file name of the file to be written, and the second parameter is the string to be written.
The following example writes the name to a new file named "newfile.txt":
Example
<?php $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = "Bill Gates\n"; fwrite($myfile, $txt); $txt = "Steve Jobs\n"; fwrite($myfile, $txt); fclose($myfile); ?>
Please note that we write the name to the file "newfile.txt" Wrote it twice. Each time we write to the file, the string $txt we send contains "Bill Gates" the first time and "Steve Jobs" the second time. After writing is complete, we use the fclose() function to close the file.
If we open the "newfile.txt" file, it should look like this:
Bill Gates Steve Jobs
For more PHP knowledge, please visit the PHP Chinese website PHP Tutorial!
The above is the detailed content of How to write files in php. For more information, please follow other related articles on the PHP Chinese website!