PHP Create file - fopen()
##fopen () function is also used to create files. Maybe it's a little confusing, but in PHP, the functions used to create files are the same as those used to open files.
If you use fopen() to open a file that does not exist, this function creates the file, assuming the file is opened for writing (w) or adding (a). (Recommended learning: PHP Programming from entry to proficiency)
The following example creates a new file named "testfile.txt". This file will be created in the same directory as the PHP code:Instance
$myfile = fopen("testfile.txt", "w")
PHP File Permissions
If An error occurred while you were trying to run this code. Please check if you have PHP file access to write information to your hard drive.PHP Write File - 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); ?>
If we open the "newfile.txt" file, it should look like this:
Bill Gates Steve Jobs
The above is the detailed content of How to create php file. For more information, please follow other related articles on the PHP Chinese website!