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"!