Home > Article > Backend Development > PHP directory handling—open/close directories
PHP Directory Processing - Open/Close Directory
What is directory processing?
Directory is a special kind of file. To browse the files in the directory, the first thing to do is to open the directory. After browsing, you must also close the directory. Directory processing includes opening the directory, browsing the directory, and closing the directory.
In the previous article, we introduced PHP file processing. At that time, directory processing was similar to file processing. They both required opening and browsing. The process is the same, but the functions used are different. There are Articles about PHP file processing:
《PHP file processing—open/close file》
《PHP file processing—read file (one character, String)》
《PHP file processing—how to read files》
《PHP file processing—writing files and operating files》
The above articles introduce PHP file processing and common functions in detail. Friends, you can take a look. Then our article will introduce to you PHP directory processing, opening and closing directories!
1: Open the directory
Opening/closing a directory is similar to opening/closing a file, but if the opened file does not exist, it will A new file is automatically created, and if the directory opened is incorrect, an error will definitely be reported!
PHP uses the opendir() function to open a directory. The syntax format of this function is as follows:
resource opendir ( string $path [, resource $context ] )
The parameter path of the function opendir() is a legal directory path. After successful execution, it returns to the directory. If the path is not a legal directory or the directory cannot be opened due to permissions or file system errors, it will return false and generate an E_WARNING level error message. You can add the "@" symbol in front of opendir() to block the error message. Output.
2: Close the directory
Use the closedir() function to close the directory. The function syntax is as follows:
void closedir ([ resource $dir_handle ] )
Parameter handle A directory pointer opened using the opendir() function.
The following example is the process code for opening and closing a directory. The specific example code is as follows:
<?php header("Content-Type:text/html; charset=utf-8"); $path = "D:\phpStudy\WWW\php"; if(is_dir($path)){ //检测是否是一个目录 if($dire = opendir($path)){ //判断打开目录是否成功 echo $dire; //输出目录指针 } } else{ echo "路径错误"; exit(); } .... //其他操作 closedir($dire); //关闭目录 ?>
is_dir() function determines whether the current path is a legal directory. If it is legal, it returns true , otherwise return false.
Opening/closing the directory is introduced here. In the next article, we will introduce the browsing directory and operating directory. For details, please read "PHP Directory Processing—Browsing Directory and Operating Directory"!
The above is the detailed content of PHP directory handling—open/close directories. For more information, please follow other related articles on the PHP Chinese website!