Simple method to use php closedir() function?

怪我咯
Release: 2023-03-13 12:28:01
Original
1477 people have browsed it

closedir() FunctionClose the directory handle.

Syntax

 closedir(dir_handle);
Copy after login

Parameters dir_handle Optional. Specifies the directory handle resource previously opened by opendir(). If this parameter is not specified, the last link opened by opendir() is used.

Usage example, PHP opendir() is used to open a directory or folder. The directory to be opened must exist physically, otherwise an error will be reported. opendir() is commonly used in conjunction with closedir(), readdir() and rewinddir(). PHP opendir() returns true if executed successfully, false if failed. The following is an example for reference:, the code is as follows:

<?php

$dir = "/upload/";//定义要打开的目录为“upload”

//打开目录并遍历所有文件

if (is_dir($dir)) {

 if ($dh = opendir($dir)) {

     while (($file = readdir($dh)) !== false) {

          echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";

     }

      closedir($dh);

  }

}

?>
Copy after login

The above code will list all file names in the upload folder, if included Chinese file names may be garbled, so you need to output the header of the PHP web page: opendir()

header("content-type:text/html;charset=utf-8");
Copy after login

After adding this sentence, the Chinese file names will not be garbled. Remember, after using opendir() to open a directory, use closedir() to close it. This is the best habit in PHP programming.

The above is the detailed content of Simple method to use php closedir() function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!