PHP Directory Processing-open/close directory

Source: Internet
Author: User
Tags file handling
PHP Directory processing-open/close directory

What is directory processing?

Directory is a special kind of file, to browse the file under the directory, the first thing is to open the directory, browse finished, the same to close the directory. Directory processing includes opening directories, browsing directories, and closing directories.

In the previous article we introduced the PHP file processing, when the directory processing and file processing is similar, are to open, browse close, this process is the same, just the function used is not the same, there are articles about PHP file processing:

PHP file Processing-open/close files

PHP file Processing-read files (one character, string)

"PHP File Handling-How to read files"

"PHP file processing-write files and operation files"

The above several articles in detail the PHP file processing and commonly used functions, small partners can see, then we this article for you to introduce PHP directory processing, open and close the directory!

One: Open Directory

Opening/Closing the directory is similar to opening/closing a file, but if the open file does not exist, a new file will be created automatically, and if the Open directory is incorrect, it will be an error!

PHP uses the Opendir () function to open the directory, which has the following syntax format:

Resource Opendir (String $path [, Resource $context])

The parameter of the function Opendir () path is a valid directory path, returns a pointer to the directory after successful execution, returns false if path is not a valid directory, or fails to open the directory because of a permission or file system error, and generates a e_ Warning level of error message, you can then Opendir () preceded by the "@" symbol to mask the output of the error message.

Two: Close the directory

Close the directory using the Closedir () function, the function syntax is formatted as follows:

void Closedir ([resource $dir _handle])

The parameter handle is a directory pointer opened with the OPENDIR () function.

The following example is the process code for opening and closing a directory, with the following sample code:

<?phpheader ("content-type:text/html; Charset=utf-8 "), $path =" D:\phpStudy\WWW\php ", if (Is_dir ($path)) {                     //detect if it is a directory    if ($dire = Opendir ($path)) {        //Determine if Open Directory is successful        echo $dire;                    Output directory Pointer    }}   else{    echo "path error";    Exit ();} ....                                   Other Operation Closedir ($dire);                       Close Directory?>

The Is_dir () function determines whether the current path is a valid directory, and returns True if it is valid, otherwise false.

Open/Close the directory is introduced here, the next article we introduce the Browse directory and operation directory, please read the "PHP Directory processing-Browse directory and operation directory"!

Related Article

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.