Question

<?php
$dir = '';

if(isset($_GET['dir']) && !empty($_GET['dir']) && !stristr($_GET['dir'], '.')){
    $dir = '/'. $_GET['dir'];
    echo '<a href="files.php">back to /</a><br><br>';
}

foreach(scandir('files'. $dir) as $filename){
    if($filename == ".." || $filename == "."){
        // do nothing. We want files and folders, not dots.
    }
    else
    {   
        if(is_file($filename)){
            echo '<a href="files/'. $filename .'">'. $filename .'</a><br>';
        }
        else
        {
            echo '<a href="files.php?path='. $filename .'">'. $filename .' (dir)</a><br>';
        }
    }
}
?>

So I want to make a file indexer for my site, just like you would see in windows explorer (NOT ie!). But the script I currently have just detects the one folder in the files/ directory (were all the files are that I want to index) and completely misses the files, in or outside of the sub-folder. And when I click on the sub folder, it keeps acting like its in the main folder (files/).

How do I solve this?

Another question, should I use glob or scandir? It's only going to contain ~80 files so I won't have to deal with the memory_limit problem like it notes on the PHP.net page:

Don't use glob() if you try to list files in a directory where very much files are stored (>100.000). You get an "Allowed memory size of XYZ bytes exhausted ..." error. You may try to increase the memory_limit variable in php.ini. Mine has 128MB set and the script will still reach this limit while glob()ing over 500.000 files.


Updated code after Barmar's solution:

<?php
            $dir = '';

            if(isset($_GET['dir']) && !empty($_GET['dir']) && !stristr($_GET['dir'], '.')){
                $dir = $_GET['dir'];
                echo '<a href="files.php">Back to /</a><br><br>';
            }

            foreach(scandir('files'. $dir) as $filename){
                if($filename == ".." || $filename == "."){
                    // do nothing. We want files and folders, not dots.
                }
                else
                {   
                    if(is_file($dir .'/'. $filename)){
                        echo '<a href="'. $filename .'">'. $filename .'</a><br>';
                    }
                    else
                    {
                        echo '<a href="files.php?dir='. $dir .'/'. $filename .'">'. $filename .' (dir)</a><br>';
                    }
                }
            }
        ?>
Was it helpful?

Solution

scandir returns filenames, not pathnames, so you need to add the directory prefix:

foreach(scandir('files'. $dir) as $filename){
    if($filename == ".." || $filename == "."){
        // do nothing. We want files and folders, not dots.
    }
    else
    {   
        if(is_file($filename)){
            echo '<a href="files/'. $dir . '/' . $filename .'">'. $filename .'</a><br>';
        }
        else
        {
            echo '<a href="files.php?dir=' . $dir . '/' . $filename .'">'. $filename .' (dir)</a><br>';
        }
    }
}

You were also missing files.php? in the first link, and the second link should use dir= to make it recurse into the subdirectory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top