Home  >  Article  >  Backend Development  >  How to use opendir in PHP

How to use opendir in PHP

angryTom
angryTomOriginal
2019-08-24 10:04:402578browse

How to use opendir in PHP

PHP opendir() function

Example

<?php
$dir = "/images/";

// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>

Result

filename: cat.gif
filename: dog.gif
filename: horse.gif

Definition and usage

The opendir() function opens a directory handle.

Syntax

opendir(path,context);
Parameters Description
path Required. Specifies the directory path to be opened.
context Optional. Specifies the environment for directory handles. context is a set of options that modify the behavior of the directory stream.

Technical details

##Return value: If successful, the directory handle resource is returned. Returns FALSE on failure. If the path is not a legal directory, or the directory cannot be opened due to licensing restrictions or file system errors, an error of level E_WARNING is thrown. You can hide the error output of opendir() by adding '@' before the function name. PHP Version: 4.0 PHP Change Log: PHP 5.0: path parameter support
ftp:// URL encapsulation protocol.

The above is the detailed content of How to use opendir 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
Previous article:The role of php commentsNext article:The role of php comments