PHP directory functions

PHP directory handling

PHP provides directory functions for opening folders, reading entries, checking paths, creating directories and removing empty directories. Start with the simplest operation you need, and validate every path before using it.

$dir = __DIR__ . '/uploads';
if (is_dir($dir)) {
    foreach (scandir($dir) as $entry) {
        if ($entry !== '.' && $entry !== '..') {
            echo htmlspecialchars($entry) . '<br>';
        }
    }
}

The examples below show additional variations and practical file-handling cases.

Functions Description
scandirList of All files and directories present inside a directory
getcwdGet Current working directory details
chdirChange the current directory to a new directory

opendir():
Open the directory handle, returns False on failure.
readdir():
Read entry from directory handle.
closedir():
Close directory handle
Listing files and directories inside a directory

dirname(): get the parent path

dirname() returns the parent portion of a path string. It does not check whether that path exists.
$var='https://www.plus2net.com/sql_tutorial/math.php';
echo dirname($var);
Output is here
https://www.plus2net.com/sql_tutorial

is_dir(): check whether a path is a directory

is_dir() returns true only when the supplied path exists and is a directory.
$var='test_dir';
if(is_dir($var)){
echo "This is a directory : $var ";
}else{
echo "This is NOT a directory : $var ";
} 
PHP File handling


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer