php目录资源管理器脚本帮助

时间:2010-12-12 19:36:10

标签: php directory

我创建了这个php脚本,它显示指定目录的内容,并允许用户下载每个文件。这是代码:

<?php  
if ($handle = opendir('test')) {  
     while (false !== ($file = readdir($handle))) {  
        if ($file != "." && $file != "..") {  
            echo "<a href='test/$file'>$file\n</a><br/>";  
        }  
    }  
    closedir($handle);  
}
?>

此脚本还显示文件夹,但是当我单击文件夹时,它会显示文件夹的内容,但是在默认的Apache自动索引视图中。

单击文件夹时我希望脚本执行的操作是显示内容,但是与原始脚本的方式相同(因为css等更可编辑)。 你知道如何实现这个目标吗?

2 个答案:

答案 0 :(得分:3)

不要创建指向目录本身的链接,而是创建显示内容的php页面。

将您的PHP代码更改为:

if(isset($_REQUEST['dir'])) {
    $current_dir = $_REQUEST['dir'];
} else {
    $current_dir = 'test';
}

if ($handle = opendir($current_dir)) {
     while (false !== ($file_or_dir = readdir($handle))) {
        if(in_array($file_or_dir, array('.', '..'))) continue;
        $path = $current_dir.'/'.$file_or_dir;
        if(is_file($path)) {
            echo '<a href="/Implementation/'.$path.'">'.$file_or_dir."\n</a><br/>";
        } else {
            echo '<a href="script.php?dir='.$path.'">'.$file_or_dir."\n</a><br/>";
        }
    }
    closedir($handle);
}

PS用双引号写你的html代码。

答案 1 :(得分:1)

您需要HREF指向PHP脚本,而不是目录。然后,您需要将PHP脚本更新到现在需要读取的目录。