Here is my Code :::

 <?php

        if ($handle = opendir('images/')) {
            echo "Directory handle: $handle\n";
            echo "Entries:\n";


            while (false !== ($entry = readdir($handle))) {
                echo "$entry\n";?><br/>
            <?php }

            /* This is the WRONG way to loop over the directory. */
            while ($entry = readdir($handle)) {
                echo "$entry\n";
            }

            closedir($handle);
        }
  ?>

Output : login.php

loop1.php
My Music
My Pictures
note.xml
saveeditor.php
select.php 

In this listing MY Music is folder but i did not open the folder.... Any solution for this particular problem.... 

Dani AI

Generated

Short answer: render folder names as links that pass a directory parameter to the script, validate that parameter, then list only that folder. This follows 's note that a folder must be opened to list it and builds on 's idea of drill-down — but keep navigation driven by a safe ?dir= value instead of automatically recursing everything.

Key points to implement

  • Fix a single base path (for example images/) and resolve it with realpath() so browsing is limited to that subtree.
  • Accept a dir GET parameter, normalize it (trim slashes), then build realpath($base . '/' . $dir) and reject the request if the result is outside $base (prevents path traversal).
  • Use scandir() (or similar) to list entries, skip . and .., use is_dir() to detect folders, and render folders as links like ?dir=some/sub/folder while displaying files as plain names or download links.
  • Always escape output with htmlspecialchars() and encode URL parts with rawurlencode().

Example (minimal, safe-ish) implementation:

<?php
$base = realpath(__DIR__ . '/images');
$req = isset($_GET['dir']) ? trim($_GET['dir'], "/\\") : '';
$target = realpath($base . DIRECTORY_SEPARATOR . $req);

if ($target === false || substr($target, 0, strlen($base)) !== $base) {
    echo "Invalid directory";
    exit;
}

if ($req !== '') {
    $up = rawurlencode(dirname($req));
    echo '<a href="?dir=' . $up . '">.. (up)</a><br>';
}

$items = array_diff(scandir($target), ['.', '..']);
foreach ($items as $item) {
    $full = $target . DIRECTORY_SEPARATOR . $item;
    $display = htmlspecialchars($item, ENT_QUOTES, 'UTF-8');
    if (is_dir($full)) {
        $param = rawurlencode(trim($req . '/' . $item, '/'));
        echo '<a href="?dir=' . $param . '">' . $display . '/</a><br>';
    } else {
        echo $display . '<br>';
    }
}
?>

Security/troubleshooting notes

  • Never trust raw $_GET['dir']: always use realpath() and a prefix check to confine navigation to the intended base. See realpath().
  • Escape output to avoid XSS: use htmlspecialchars(). Use scandir() or DirectoryIterator if preferred.
  • Be cautious with symlinks, hidden files, and serving executable files. For path-traversal context, refer to the OWASP note on Path Traversal.

Recommended Answers

All 7 Replies

Each new folder, you want to list the contents of, needs to opened with opendir as well. It's a recursive function.

function my_opendir($dir) {
  if ($handle = opendir($dir)) {
    echo "<ul>Directory handle: $dir<br />Entries:";

    while (false !== ($entry = readdir($handle))) {
      if($entry=="."||$entry=="..") { continue; }
      echo "<li>";
      if(is_dir($dir.$entry)) { my_opendir($dir.$entry); }
      else { echo $entry; }
      echo "</li>";
      }
    echo "</ul>";

    closedir($handle);
    }
  }

:: Thaxs... Bur when when i click on particular folder then and then only open this folder ...

Hi..
How to click any file or folder in dirtectory listing ?
Any Solytion for this ?

in line 9 print entry as link, like: "<a href="".$entry."">".$entry."</a>"

Hello i want open only folder in directory listing not a file ?
There is any script avaiable in php ?

Maybe samthing like this:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.