Search code examples
phpdirectoryscandirchdir

How to change directory for scandir on link


I want to small script to print the content of the current directory with scandir. The idea was to made a small tool for filenavigation with preview for the directory the php file is stored in and the possiblity to navigate through the directory.

The problem is I dont know how to change the directory for scandir (via link) maybe with getpost and refresh the file to output the content.

actually my script is only working if i put it in every directory I want to navigate through (and go back). Is there a way to get this working by only having it in the directory where I want to start the navigation and change the directory by clicking on a folder?

I've tried to make a variable to forward post value and making the directory links on the current output like chdir(directory) and adding a if/else on top which checks if there is content in getpost and if then change the dir to this instead of the current directory.. but this only works for 2 folders up an then stops working..

<?php
$dir = '.';
$directories = array();
$files_list  = array();
$files = scandir($dir);

foreach ($files as $file) {
    if(($file != '.') && ($file != '..') && ($file != 'index.php') && ($file != 'assets')) {
    if(is_dir($dir.'/'.$file)){
        $directories[] = $file;
        }
        else {
        $files_list[] = $file;
        }
    }
}

// Print Directory to screen
foreach ($directories as $directory) {
    echo "<div class=\"wrapper\">";
    echo "<a href=\"" . $directory . "\">";
    echo "<div class=\"preview\">";
    echo "<img src=\"assets/media/ordner.png\" alt=\"directory\" class=\"packshot zoom\" />";
    echo "</div>";
    echo "</a>";
    echo "<div class=\"title\"><p>&raquo; " . $directory . "</p></div>";
    echo "</div>";
}

// print files
foreach ($files_list as $file_list) {
    $file_list_info = pathinfo($dir."/".$file_list);
    $file_list_size = ceil(filesize($dir."/".$file_list)/1024);
    $file_list_pic = array("jpeg", "jpg", "png", "gif", "svg", "bmp", "tiff");

    if(in_array($file_list_info['extension'],$file_list_pic)) {
    echo "<div class=\"wrapper\">";
    echo "<a href=\"" . $file_list . "\" target=\"_blank\">";
    echo "<div class=\"preview\">";
    echo "<img src=\"" . $file_list . "\" alt=\"picture\" class=\"packshot\" />";
    echo "</div>";
    echo "</a>";
    echo "<div class=\"title\"><p>" . $file_list . " (" . $file_list_size . "kb)</p></div>";
    echo "</div>";
    }
}
 ?>

Solution

  • You could use the $_GET variable.

    At the beginning of the file you could define the dir var like that:

    if(!empty($_GET['dir'])){
        $dir = $_GET['dir'];
    }else{
        $dir = '.';
    }
    
    

    And then when you generate the links use something like that

    echo "<a href=\"?dir=" .$dir."/". $directory . "\">";
    

    So when you will click on the link it will redirect to "index.php?dir=mydir"

    And the page will show the content of "./mydir/"