Home >Backend Development >PHP Problem >How to close directory in php

How to close directory in php

青灯夜游
青灯夜游Original
2022-04-26 14:55:022183browse

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)".

How to close directory in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$dir = "img/";
// 打开一个目录,并读取其内容
if (is_dir($dir)) {
	if ($dh = opendir($dir)) {
		while (($file = readdir($dh)) !== false) {
			echo "文件名:" . $file . "<br>";
		}
		closedir($dh);
	}
}
?>

How to close directory in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn