There are 26 items you can search

PHP Show Folders in Directory | VibeScriptz

Show clickable folders in a directory with PHP. Lightweight example that lists folders containing index.php and ignores hidden folders.

Showing 2 sample folders.

contact/

Contains index.php

Updated Apr 8, 2026 11:20 AM

tools/

Contains index.php

Updated Apr 10, 2026 4:05 PM

This script scans one directory, shows folders only, requires index.php inside each folder, ignores hidden and common system folders, and can show the last modified date of each folder’s index.php.

Folder links are relative to the directory where the script is installed. That means the script works correctly whether it is placed in the website root or inside a subdirectory.

For example, if the script is installed at /photos/folders.php and it finds a folder named gallery-one, the generated link will correctly point to /photos/gallery-one/.

It can also be marked noindex for private use when you do not want the generated folder list indexed by search engines.

This example is a good fit for quick private folder indexes, internal tools, photo directories, dev or staging environments, simple utility pages, and lightweight public examples where you want to show only folders that contain an index.php file.

It works best when you want a small drop-in script that stays easy to understand, easy to edit, and easy to keep under control without pulling in a larger indexing system.

Save this as something like folders.php in the directory whose folders you want to list.

If the folders you want to list are inside a subdirectory, place folders.php inside that same directory.

<?php
declare(strict_types=1);

/*
 * Save this file in the directory whose folders you want to list.
 * Shows only visible folders that contain index.php.
 * Folder links stay relative to the directory where this script is installed.
 * PHP 8.4 compatible.
 */

header('Content-Type: text/html; charset=UTF-8');
header('X-Robots-Tag: noindex, nofollow, noarchive, nosnippet', true);

$rootPath = __DIR__;

$skipFolders = [
    'cgi-bin',
    '.well-known',
    'vendor',
    'node_modules',
    '.git',
    '.svn',
    '__pycache__',
];

function h(string $value): string
{
    return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}

function isHiddenOrSkipped(string $name, array $skipFolders): bool
{
    if ($name === '.' || $name === '..') {
        return true;
    }

    if (str_starts_with($name, '.')) {
        return true;
    }

    return in_array(strtolower($name), array_map('strtolower', $skipFolders), true);
}

$folders = [];

$items = scandir($rootPath);

if ($items !== false) {
    foreach ($items as $item) {
        if (isHiddenOrSkipped($item, $skipFolders)) {
            continue;
        }

        $fullPath = $rootPath . DIRECTORY_SEPARATOR . $item;

        if (!is_dir($fullPath)) {
            continue;
        }

        $indexFile = $fullPath . DIRECTORY_SEPARATOR . 'index.php';

        if (!is_file($indexFile)) {
            continue;
        }

        $folders[] = [
            'name' => $item,

            /*
             * No leading slash here.
             *
             * This keeps the link relative to the directory
             * where folders.php is installed.
             *
             * Example:
             * /photos/folders.php
             *
             * Folder:
             * gallery-one
             *
             * Link becomes:
             * /photos/gallery-one/
             */
            'url' => rawurlencode($item) . '/',

            'modified' => @filemtime($indexFile) ?: null,
        ];
    }
}

usort(
    $folders,
    static fn(array $a, array $b): int => strcasecmp($a['name'], $b['name'])
);

$totalFolders = count($folders);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="robots" content="noindex,nofollow,noarchive,nosnippet">
<title>Folder Links (<?php echo $totalFolders; ?>)</title>
<style>
body{
    margin:0;
    background:#111;
    color:#eee;
    font:16px/1.5 Arial,Helvetica,sans-serif;
}
.wrap{
    max-width:760px;
    margin:40px auto;
    padding:24px;
}
h1{
    margin:0 0 18px;
    font-size:24px;
}
.note{
    color:#aaa;
    margin:0 0 20px;
}
.count{
    margin:0 0 20px;
    color:#7db8ff;
    font-weight:bold;
}
.list{
    display:grid;
    gap:10px;
}
a{
    display:block;
    padding:12px 14px;
    background:#1b1b1b;
    border:1px solid #2a2a2a;
    border-radius:10px;
    color:#7db8ff;
    text-decoration:none;
}
a:hover{
    background:#222;
    border-color:#3a3a3a;
}
.row{
    display:flex;
    justify-content:space-between;
    align-items:center;
    gap:12px;
}
.folder-name{
    font-weight:bold;
    word-break:break-word;
}
.folder-date{
    color:#aaa;
    font-size:13px;
    white-space:nowrap;
}
.empty{
    color:#aaa;
    padding:12px 14px;
    background:#1b1b1b;
    border:1px solid #2a2a2a;
    border-radius:10px;
}
@media (max-width:600px){
    .row{
        display:block;
    }

    .folder-date{
        display:block;
        margin-top:4px;
        white-space:normal;
    }
}
</style>
</head>
<body>
<div class="wrap">

    <h1>Folders with index.php</h1>

    <p class="note">
        Only visible folders containing an <code>index.php</code> file are shown.
        Date shown is the last modified date of that file.
    </p>

    <p class="count">
        Showing <?php echo $totalFolders; ?>
        folder<?php echo $totalFolders === 1 ? '' : 's'; ?>
    </p>

    <div class="list">

        <?php if (!empty($folders)): ?>

            <?php foreach ($folders as $folder): ?>

                <a href="<?php echo h($folder['url']); ?>">

                    <span class="row">

                        <span class="folder-name">
                            <?php echo h($folder['name']); ?>/
                        </span>

                        <span class="folder-date">

                            <?php if ($folder['modified']): ?>

                                Updated <?php echo date('M j, Y g:i A', $folder['modified']); ?>

                            <?php else: ?>

                                Date unavailable

                            <?php endif; ?>

                        </span>

                    </span>

                </a>

            <?php endforeach; ?>

        <?php else: ?>

            <div class="empty">
                No matching folders found.
            </div>

        <?php endif; ?>

    </div>

</div>
</body>
</html>

If you do not want the folder-listing page crawled, add its actual path to robots.txt.

For a root install:

User-agent: *
Disallow: /folders.php

For a subdirectory install:

User-agent: *
Disallow: /photos/folders.php

This script does not crawl nested folders recursively, does not check for files other than index.php, and is not meant to be a full file manager or public directory browser for sensitive paths.

If you use it on a real site, keep the allowed scope tight, skip folders you do not want exposed, and leave it noindex unless you have a specific reason to make the output public.

For a real install, this script should usually stay private and unindexed. The public page here is a styled example for copy-paste use.

The folder links are intentionally relative rather than starting with /. This lets the same script work correctly whether it is installed in the website root or inside another directory.