building a simple directory browser using php RecursiveDirectoryIterator

后端 未结 1 814
生来不讨喜
生来不讨喜 2020-12-18 17:19

Hi i am trying to build simple directory browser to browse folders and sub-folders uing php RecursiveDirectoryIterator .. i need help of how to create this. i have started w

相关标签:
1条回答
  • 2020-12-18 17:39

    Allow me to code that for you....

    <?php
    $root = __DIR__;
    
    function is_in_dir($file, $directory, $recursive = true, $limit = 1000) {
        $directory = realpath($directory);
        $parent = realpath($file);
        $i = 0;
        while ($parent) {
            if ($directory == $parent) return true;
            if ($parent == dirname($parent) || !$recursive) break;
            $parent = dirname($parent);
        }
        return false;
    }
    
    $path = null;
    if (isset($_GET['file'])) {
        $path = $_GET['file'];
        if (!is_in_dir($_GET['file'], $root)) {
            $path = null;
        } else {
            $path = '/'.$path;
        }
    }
    
    if (is_file($root.$path)) {
        readfile($root.$path);
        return;
    }
    
    if ($path) echo '<a href="?file='.urlencode(substr(dirname($root.$path), strlen($root) + 1)).'">..</a><br />';
    foreach (glob($root.$path.'/*') as $file) {
        $file = realpath($file);
        $link = substr($file, strlen($root) + 1);
        echo '<a href="?file='.urlencode($link).'">'.basename($file).'</a><br />';
    }
    
    0 讨论(0)
提交回复
热议问题