Tag Archives: Directory

scandir

it lists files and directories of a given path.

Example

<?php
echo “<pre>”;
$dir    = ‘/var/www/test/’;
$files1 = scandir($dir);
print_r($files1);
?>

Output

Array
(
[0] => .
[1] => ..
[2] => index.php
[3] => enc.php
)

rewinddir

It rewinds directory handle and can be used to check for changes in a directory.

Example

<?php
$dir = opendir(“/var/www/test/”);
while (($file = readdir($dir)) !== false)
echo  $file . “<br />”;
rewinddir($dir);
closedir($dir);
?>

Output

index.php
..
.

dir

It return an instance of the Directory class

Example

<?php
$dir = dir(“/var/www/test/date/”);
echo “Handle: ” . $dir->handle . “<br />”;
echo “Path: ” . $dir->path . “<br />”;
while (($val = $dir->read())  !== false) {
echo $val.”<br />”;
}
$dir->close();
?>

Output

Handle: Resource id #3
Path: /var/www/test/date/
index.php
enc.php
..
.

Directory functions

  • chdir – It can be used to change directory
  • chroot – It can be used to change the root directory
  • closedir – It closes directory handle
  • dir – It return an instance of the Directory class
  • getcwd – It returns the current working directory
  • opendir – It opens directory handle
  • readdir – It reads entry from directory handle
  • rewinddir – It rewinds directory handle
  • scandir – It lists files and directories of a given path.