doutangtan6386 2013-06-26 19:24
浏览 53
已采纳

索引文件夹:未检测到目录

<?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>';
                    }
                }
            }
        ?>
  • 写回答

1条回答 默认 最新

  • douduan5073 2013-06-26 19:36
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀