Home  >  Article  >  Backend Development  >  Simple method to use php closedir() function?

Simple method to use php closedir() function?

怪我咯
怪我咯Original
2017-07-10 15:30:531544browse

closedir() FunctionClose the directory handle.

Syntax

 closedir(dir_handle);

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);

  }

}

?>

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

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!

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