X
HOME HTML5 CSS3 JAVASCRIPT SQL PHP5 JQUERY ANGULAR BOOTSTRAP3 XML MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

PHP opendir() Function

PHP Directory 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 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 PHP Directory Reference

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook