Details of the PHP Directory function

  • 2020-05-30 19:44:18
  • OfStack

Predefined constants:

DIRECTORY_SEPARATOR (string) : directory separator

PATH_SEPARATOR (string) : path separator

bool chdir (string $directory) - change directory

 echo getcwd() . "\n";
 echo getcwd() . "\n";

bool chroot (string $directory) - change the root directory only if the system supports and runs on CLI, CGI, or embedded SAPI versions.

dir::dir (string $directory) -- directory class, there are three methods available: read, rewind (repointing the position pointer inside the file to the beginning of a data stream), and close

$d = dir("E:/work/html");
 foreach($d as $k=>$v){
     echo $k.'->' .$v. '<br/>';
 while(false !== ($entry = $d->read())){
     echo $entry."<br/>";

void closedir (resource $dir_handle) - close the directory handle

$dir = "/etc/php5/";

 if (is_dir($dir)) {
     if ($dh = opendir($dir)){
         $directory = readdir($dh);

string getcwd (void) - gets the current working directory

resource opendir (string $path [, resource $context]) -- open directory handle

string readdir (resource $dir_handle) - reads the entry from the directory handle

if ($handle = opendir('/path/to/files')) {
     echo "Directory handle: $handle\n";
     echo "Files:\n";
     while (false !== ($file = readdir($handle))) {
         echo "$file\n";

void rewinddir (resource $dir_handle) - resets the directory stream specified by dir_handle to the beginning of the directory

array scandir (string $directory [, int $sorting_order [, resource $context]]) - lists the files and directories in the specified path

 $dir    = '/tmp';
 $files1 = scandir($dir);
 $files2 = scandir($dir, 1);

Related articles: