Before you work with file content, you typically need to establish a connection to a file resource, which is to open the file. Also, after you end the operation of the resource, you should close the connection resource. The so-called open file, is actually to create a variety of information about the file, and so that the file pointer to the file, you can initiate the input or output of the entity linked together, also prohibit the operation of the file. In PHP, you can establish a connection to a file resource through a standard function fopen (), and use the fclose () function to turn off file resources opened through the fopen () function.
① function fopen ()
This function is used to open a file, and when you open a file, you also need to specify if you want to use it. Which is the file mode in which to open the file resource. The operating system files on the server must know what to do with the open files. The operating system needs to know whether the file also allows other program scripts to open after the file is opened, and whether the owner of the script has permission to use the file in this manner. The prototype of the function looks like this:
Copy Code code as follows:
Resource fopen (string filename,string mode[,bool use_include_path[,resource Zcontext])//Open File
The first parameter needs to provide the URL of the file to be opened. This URL can be an absolute path in the server where the script resides, a relative path, or a file in a network resource. The second parameter needs to provide a file pattern that tells the operating system how to handle access requests from other people or scripts, and a way to check if you have access to the particular file. When you open a file, you have three choices:
★ Open a file for read-only, write-only, or read-write.
★ If you want to write a file, you can overwrite all existing file contents, or you need to append the new data to the end of the file.
★ If you write a file on a system that distinguishes between binary and plain text files, you must also specify the way in which to use it.
function fopen () also supports the combination of the above three-room Sihai, simply provide a string in the second argument, specifying what will be done to the file. The following table lists the file patterns that you can use and their meaning.
The third parameter is optional, and if the resource is on the local file system, PHP thinks it can access the resource using either a local path or a relative path. If you set this parameter to 1, this will cause PHP to consider the path specified in the configuration instruction Include_path (set in the PHP configuration file).
The fourth parameter is optional, and the fopen () function allows the file name to start with the protocol name, such as "http://", and opens the file in a remote location. By setting this parameter, you can also support some other protocols.
If the fopen () function successfully opens a file, the function returns a file pointer to the file. The read, write, and other file action functions that are used to manipulate the file use this resource to access the file. Returns False if the file failed to open. The function fopen () is an example of yo on:
Copy Code code as follows:
<?php
Open the File.txt file using an absolute path, select Read-only mode, and return the resource $handle
$handle = fopen ("/home/rasmus/file.txt", "R");
Access files under the root of the document, also open in read-only mode
$handle = fopen ("$_server[' Document_root ']/data/info.txt", "R");
On the Windows platform, escape each backslash of the file path, or use a slash to combine in binary and write-only mode
$handle = fopen ("C:\\data\\file.gif", "WB");
Open the File.txt file using a relative path, select Read-only mode, and return the resource $handle
$handle = fopen (". /data/info.txt "," R ");
Open a remote file and use the HTTP protocol to open only in read-only mode
$handle = fopen ("http://www.example.com", "R");
Use the FTP protocol to open a remote file, which can be opened in write mode if the FTP server is writable
$handle = fopen ("ftp://user:password@example.com", "w");
?>
② function fclose ()
The resource type is one of the basic types of PHP, and once the resource is processed, it must be turned off, or some unexpected error may occur. The function fclose () revokes the resource type that fopen () opens, returns True when it succeeds, or returns false. The parameter must be a saved file pointer opened using the fopen () or Fsockopen () function. The Opendir () function in directory operations also opens a resource and closes it with Closedir ().