php recursiv函数无法使用按钮

时间:2017-08-01 12:52:57

标签: php recursion filetree

我想用PHP编写文件树,我可以一次完成简单的完整树:

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>';

    }
}

问题是我使用表单并提交按钮而不是树($ new_path); 它将适用于第一次扫描,如果我点击一个按钮它将工作,但就是这样,它不会再进一步​​...... 示例:

$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>";
    }
}

拜托,有什么建议吗?我知道它可能有些不兼容但我是新的:) 而且我试图找到类似的东西,但我不能,如果它重复的话就不会发生。

0 个答案:

没有答案