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>';
}
}
}This example shows the recommended starting pattern. Continue below for variations, explanation and the original practical examples.
| Functions | Description |
|---|---|
| scandir | List of All files and directories present inside a directory |
| getcwd | Get Current working directory details |
| chdir | Change the current directry to new directory |
opendir():readdir():closedir():$var='https://www.plus2net.com/sql_tutorial/math.php';
echo dirname($var);
Output is here
https://www.plus2net.com/sql_tutorial
$var='test_dir';
if(is_dir($var)){
echo "This is a directory : $var ";
}else{
echo "This is NOT a directory : $var ";
}
PHP File handling
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.