PHP does not have a separate file creation function, if we want to create a function, you can use fopen (), the fopen () function literally means to open the file, but the function also has the ability to create a file, when using the fopen () function to open a file, if the file does not exist, it will attempt to create the file, and returns a resource.
Introduction to PHP fopen functions
fopen function to open a file or URL
Grammar:
Resource fopen (string filename, string mode)
fopen () binds the name resource specified by filename to a stream.
Parameters:
1. FileName is the name of the file you are trying to open/create.
If filename is "scheme://..." Format, the Search protocol processor (also known as the Encapsulation Protocol) is treated as a url,php to handle this pattern. If the protocol has not yet been registered for encapsulation, PHP will issue a message to help examine potential problems in the script and continue with filename as a normal file name.
If PHP thinks that filename specifies a local file, it will attempt to open a stream on that file. The file must be accessible by PHP, so you need to confirm that the file access permission allows that access. If Safe mode is activated or open_basedir is applied, further restrictions apply.
If PHP believes that filename specifies a signed protocol, and that the protocol is registered as a network url,php will check and confirm that Allow_url_fopen has been activated. If it is off, PHP will issue a warning, and fopen's call fails.
2. Mode specifies the open mode with the following possible values:
PHP fopen Function Instance
1. Create a file using the fopen function:
$my _file = ' file.txt ';//If the file does not exist (default is the current directory) $handle = fopen ($my _file, ' W ') or Die (' Cannot open file: '. $my _file); Implicitly creates file
2. Use the fopen function to open the file:
$my _file = ' file.txt ';//Suppose the file file.txt exists $handle = fopen ($my _file, ' W ') or Die (' Cannot open file: '. $my _file); Open file for writing (' W ', ' R ', ' a ') ...
3, fopen function combined with fread read file:
$my _file = ' file.txt '; $handle = fopen ($my _file, ' R '); $data = Fread ($handle, FileSize ($my _file));
4, fopen function with fwrite function write file
$my _file = ' file.txt '; $handle = fopen ($my _file, ' W ') or Die (' Cannot open file: '. $my _file); $data = ' The ' the data '; Fwri Te ($handle, $data);
5. The fopen function, combined with the Fwrite function, appends the contents to the file:
$my _file = ' file.txt '; $handle = fopen ($my _file, ' a ') or die (' Cannot open file: '. $my _file); $data = ' New data line 1 '; Fwrit E ($handle, $data); $new _data = "\ n". ' New data Line 2 '; fwrite ($handle, $new _data);
6. The fopen () function can also be used to open the URL address on the Internet:
<?PHP$FH = fopen ("http://www.baidu.com/", "R"), if ($FH) {while (!feof ($fh)) { echo fgets ($FH); }}? >
Note: fopen () returns only one resource, and you need to read and output the fgets () function to display the open page address.
Through this article hope to help everyone, thank you for the support of this site!