php recursiv function not working with button

46 Views Asked by At

I wanted to do I file tree in PHP, and I can do it its simple full tree at once:

function tree($path)
{
   //scandir creates the array with all the files in the path folder
    $files = scandir($path);
    //going through each file or folder
    foreach($files as $file) {
        echo "<ul>";
    //here i am checking if $file is directory, and if it is i want to open to start same function again, and if it is not i just want download link (that works)
        if(is_dir($path . $file)) {
            if($file != '.' && $file != '..') {
                $new_path = $path . $file . "/";
                echo "<li>" . $file . "_mapa" . "</li>";
                tree($new_path);
            }
        } else {
            echo '<li><a href="' . $new_path = $path . $file . '">' . $file . '</a></li>';
        }
        echo '</ul>';

    }
}

The problem is when I use form and submit button instead of tree($new_path); it will work for the first scan and if I click on a button it will work, but that is it, it won't go any further... Example:

$dir = 'dokumenti/Arduino_programi/';
scan($dir);
function scan($path)
{
    //echo $path;
    $files = scandir($path);
    foreach($files as $items => $file) {
        echo "<ul>";
        if(is_dir($path . $file)) {
            if($file != '.' && $file != '..') {
                $new_path = $path . $file . '/';
                echo '<form  name="scan" method="post">';
                echo '<li><button  type="submit" name="scan" value="' . $new_path . '">' . $file . '</button></li>';
                echo '</form>';

                if($_POST['scan'] == $new_path) {
                    echo $new_path;
                    $i = $_POST['scan'];
                    scan($i);
                    unset($_POST);
                }
                // echo '<a href="scan('.$new_path.')">'.$file.'</a>';
            }
        } else {
            if($file != '.' && $file != '..') {
                $path_to_file = $path . $file . '/';
                $path_to_read_file = $path . $file;
                //open or dow...
                // forma za download
                echo '<form name="download i read" method="post">';
                echo '<li><button   type="submit" name="download" value="' . $path_to_file . '" >' . $file . '  download' . '</button></li>';
                $path_parts = pathinfo($file);
                if($path_parts['extension'] == 'txt') {
                    echo '<li><a target="_blank" href="' . $path . $file . '">' . $file . '     open</a>' . "<br/></li>";
                }
                echo '</form>';

            }
        }
        echo "</ul>";
    }
}

Please, any suggestions? I know it's probably some incompatibility but I am new :) And I tried to find something similar but I couldn't, sry if its duplicate.

0

There are 0 best solutions below