Home >Backend Development >PHP Problem >How to close directory in php
In PHP, you can use the closedir() function to close the directory. The function of this function is to close the opened directory handle. It accepts an optional parameter to specify the directory path that needs to be closed. If it is omitted, Use the last url link opened by opendir(); the syntax is "closedir (directory path)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use closedir () function to close the directory.
The closedir() function can close an open directory handle and accepts an optional parameter:
closedir(dir_handle);
Parameter | Description |
---|---|
dir_handle | Optional. Specifies a directory handle resource previously opened by opendir(). If this parameter is not specified, the last link opened by opendir() is used. |
Example: Open a directory using opendir(), read its contents, then close it using closedir()
<?php header('content-type:text/html;charset=utf-8'); $dir = "img/"; // 打开一个目录,并读取其内容 if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "文件名:" . $file . "<br>"; } closedir($dh); } } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to close directory in php. For more information, please follow other related articles on the PHP Chinese website!