PHP file opening and closing operation function summary

Source: Internet
Author: User
Tags ftp protocol
This article mainly introduces the summary of PHP open and close File operation functions. This article describes fopen () and fclose () functions, which focus on fopen () functions, if you need a file, you can refer to the following: before processing the file content, you usually need to establish a connection with the file resource, that is, open the file. Similarly, after the operation of the resource is completed, the connection to the resource should be closed. The so-called open file is actually to create various information about the file, and make the file pointer to the file, you can initiate an input or output object together, this prevents operations on the file. In PHP, you can use the standard function fopen () to establish a connection with file resources, and use the fclose () function to Close File Resources opened through the fopen () function.

① Function fopen ()

This function is used to open a file. when opening a file, you also need to specify if you use it. Which file mode is used to open file resources. The operating system file on the server must know what operations to perform on the opened file. The operating system needs to know whether other program scripts can be opened after the file is opened, you also need to know whether the owner of the script has the permission to use the file in this way. The function is prototype as follows:

The code is as follows:


Resource fopen (string filename, string mode [, bool use_include_path [, resource zcontext]) // open the file


The first parameter must provide the URL of the file to be opened. This URL can be an absolute path on the server where the script is located, a relative path, or a file in a network resource. The second parameter needs to provide the file mode, which tells the operating system how to handle access requests from other people or scripts, and a way to check whether you have the right to access this specific file. There are three options when opening a file:

★Open a file to read-only, write-only, or read and write.
★If you want to write a file, you can overwrite all the existing file content, or you need to append new data to the end of the file.
★If you write a file on a system that distinguishes binary files from plain text files, you must specify the method used.

The fopen () function also supports a combination of the preceding three Central and four global functions. you only need to provide a string in the second parameter and specify the operations to be performed on the file. The following table lists the available file modes and their meanings.

The third parameter is optional. if the resource is located in the local file system, PHP considers that the resource can be accessed using the local path or relative path. If this parameter is set to 1, PHP will consider configuring the path specified in the include_path command (set in the PHP configuration file ).

The fourth parameter is optional. the fopen () function allows the file name to start with the protocol name, for example, "http: //", and opens the file in a remote location. You can set this parameter to support 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 operation functions used to perform operations on the file must use this resource to access the file. If the file fails to be opened, FALSE is returned. The fopen () function is a Yo on example as follows:

The code is as follows:


<? Php
// Use absolute volume to open the File.txt file, select read-only mode, and return the resource $ handle
$ Handle = fopen ("/home/rasmus/file.txt", "r ");
// Access the file in the root directory of the document and open it 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 backslash to combine binary and write-only modes.
$ Handle = fopen ("C: \ data \ file.gif", "wb ");
// Use the relative delimiter to open the File.txt file, select the read-only mode, and return the resource $ handle
$ Handle = fopen ("../data/info.txt", "r ");
// Open a remote file. the HTTP protocol can only be opened in read-only mode.
$ Handle = fopen ("http://www.example.com", "r ");
// Use the FTP protocol to open a remote file. if the FTP server is writable, it can be opened in write mode.
$ Handle = fopen ("ftp: // user: password@example.com", "w ");
?>

② Function fclose ()

The resource type is one of the basic types of PHP. Once the resource is processed, you must disable it. otherwise, unexpected errors may occur. The fclose () function will undo the type of resource opened by fopen (). If the function is successful, TRUE is returned. otherwise, FALSE is returned. The parameter must be an existing file pointer opened by using the fopen () or fsockopen () function. In directory operations, the opendir () function also enables a resource and closes it using closedir.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.