0

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.

2
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help you debug your code. Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. Commented Aug 1, 2017 at 12:58
  • Thank you for your replay, and thank you for the tip! Commented Aug 1, 2017 at 13:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.