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

How to use opendir in PHP

angryTom
Release: 2023-04-07 10:20:01
Original
2595 people have browsed it

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);
}
}
?>
Copy after login

Result

filename: cat.gif
filename: dog.gif
filename: horse.gif
Copy after login

Definition and usage

The opendir() function opens a directory handle.

Syntax

opendir(path,context);
Copy after login
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!

Related labels:
php
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