Seo Forum

Coding & Programming => PHP Forum => Topic started by: chinmay.sahoo on 07-08-2016, 06:59:31

Title: How do I examine directories with PHP?
Post by: chinmay.sahoo on 07-08-2016, 06:59:31
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)
;
?>