How do I examine directories with PHP?

Started by chinmay.sahoo, 07-08-2016, 06:59:31

Previous topic - Next topic

chinmay.sahooTopic starter

When you're creating web-based file managers in PHP, it's handy to be able to explore the contents of directories.

Solutions

There are two basic approaches to examining directories with PHP—you should use whichever method you prefer.7

Using the readdir Function

The first approach, which uses the opendir, readdir, and closedir functions, is similar to the process of using fopen , fread, and fclose to read a file:

Quote<?php
$location = './';
$dp =
opendir($location)
;
while ($entry =
readdir($dp)
)
{
if (is_dir($location . $entry))
{
echo '[Dir] ' . $entry . '<br />';
}
else if (is_file($location . $entry))
{
echo '[File] ' . $entry . '<br />';
}
}
closedir($dp)
;
?>