PHP opendir() Function

❮ PHP Directory Reference

Example

Open a directory, read its contents, then close:

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

Parameter Values

Parameter Description
path Required. Specifies the directory path to be opened
context Optional. Specifies the context of the directory handle. Context is a set of options that can modify the behavior of a stream


Technical Details

Return Value: Returns the directory handle resource on success. FALSE on failure. Throws an error of level E_WARNING if path is not a valid directory, or if the directory cannot be opened due to permission restrictions or filesysytem errors. You can hide the error output of opendir() by adding '@' to the front of the function name
PHP Version: 4.0+
PHP Changelog: PHP 5.0: The path parameter now supports the ftp:// URL wrapper

❮ PHP Directory Reference
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.