开发者

php directory explorer script help

开发者 https://www.devze.com 2023-01-29 23:52 出处:网络
I have created this php script which displays the contents of a designated directory and allows users to download each file. H开发者_高级运维ere is the code:

I have created this php script which displays the contents of a designated directory and allows users to download each file. H开发者_高级运维ere is the code:

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

This script also displays folders, but when I click a folder, it does display the contents of the folder, but in the default Apache autoindex view.

What I would like the script to do when a folder is clicked, is display the contents, but in the same fashion as the original script does (as this is more editable with css and the like).

Would you know how to achieve this?


Don't create a link to the directory itself, but to a php page which displays the contents.

Change your php code to somthing like:

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 write you html code with double quotes.


You need your HREF to point back to your PHP script, and not the directory. You will then need to update your PHP script to now which directory it needs to read.

0

精彩评论

暂无评论...
验证码 换一张
取 消