PHP Basics PHP file operations-open a file

Source: Internet
Author: User
Tags php basics
In general, operations on a file are divided into: open a file, read or write a file, and finally close the file. Today, let's talk about the first step of file operations: opening a file. To open a file in PHP, you can use the PHP built-in function fopen. When using this function to open a file, you also need to specify how to use the file, that is, select the file mode.

In general, operations on a file are divided into: open a file, read or write a file, and finally close the file. Today, let's talk about the first step of file operations: opening a file. To open a file in PHP, you can use the PHP built-in function fopen. When using this function to open a file, you also need to specify how to use the file, that is, select the file mode.

The operating system on the WEB server must know what operations to perform on open files. The operating system needs to know whether other scripts are allowed to open the file after opening the file. it also needs to know the user (or the script owner) whether you have the permission to use the file in this way. Essentially, the file mode tells the operating system a mechanism that determines how to handle access requests from other people or scripts, and a method to check whether you have the right to access this file. When opening a file, you can choose from the following three options:

1. open the file only for read-only, write-only, read-and write purposes.

2. if you want to write a file, you may want to overwrite all the existing file content, or simply append new data to the end of the file. If the file already exists, terminate the program execution instead of overwriting the file.

3. if you want to write a file on a system with binary and plain text partitions, you must specify the method used.

The fopen () function supports single or combined operations in the preceding three methods.

1. use fopen () to open a file

Example: If a netizen wants to open a page on a novel website to read a book, the website background can call the following statement to open the file to be read:
$ Fp = fopen ("$ DOCUMENT_ROOT/../books/book1.txt", 'r ');

When calling the fopen () function, you must pass two to three parameters. Usually two parameters are used, as shown in the above code. The first parameter is the file to be opened. The above code specifies the path of a file. the variable $ DOCUMENT_ROOT specifies the root directory of the WEB server and uses ".." to indicate the parent directory of the root directory of the document. This path is called relative path because it describes a file system setting relative to the document root directory. Depending on the server settings, you can go to the root directory of the document in the following three ways:
1. $ _ SERVER ['document _ root']
2. $ DOCUMENT_ROOT
3. $ HTTP_SERVER_VARS ['document _ root']
The first style is preferred for form data.

The second parameter of the fopen () function is the file mode. it is a string that specifies the operations to be performed on the file. In the preceding example, we pass "r" to fopen ()-which means that the file must be opened as a read-only file.

If the fopen () function successfully opens a file, the function returns a file pointer to the file. In the preceding example, the file pointer is saved in the variable $ fp. When the reader does want to read this file, the variable will be used to access the file.

2. open a file through FTP or HTTP

In addition to opening a local file for read/write operations, you can also use the fopen () function to open the file through FTP, HTTP or other protocols. In the PHP configuration file php. ini, you can use the allow_url_fopen command to enable or disable this function. If you encounter problems when using this function to open a remote file, check the php. ini file.

If the file name is ftp: //, the fopen () function establishes an HTTP connection to the specified server and returns a pointer to the specified HTTP response. When using HTTP mode in earlier versions of PHP, you must add an ending slash after the directory name, as shown below:

Http://www.hostname.com/, but not http://www.hostname.com

When the last address format is used (without diagonal lines), the WEB server usually uses HTTP to rewrite to the first address (with diagonal lines ). Remember that the domain name in the URL is case-insensitive, but the path and file name may be case-sensitive. if you do not pay attention to these details, the file operation may fail.

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.