简体   繁体   English

如何更改链接的scandir目录

[英]How to change directory for scandir on link

I want to small script to print the content of the current directory with scandir. 我想用小脚本来用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. 想法是制作一个用于文件导航的小工具,该工具具有预览php文件所在目录的功能,并可以在目录中导航。

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. 问题是我不知道如何使用getpost更改scandir的目录(通过链接),并刷新文件以输出内容。

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.. 我试图做一个变量来转发帖子值,并在当前输出上进行目录链接,如chdir(directory),并在顶部添加if / else,以检查getpost中是否包含内容,然后将dir更改为此而不是当前目录..但这仅适用于2个文件夹,然后停止工作。

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

You could use the $_GET variable. 您可以使用$ _GET变量。

At the beginning of the file you could define the dir var like that: 在文件的开头,您可以像这样定义dir var:

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" 因此,当您单击链接时,它将重定向到“ index.php?dir = mydir”

And the page will show the content of "./mydir/" 然后页面将显示“ ./mydir/”的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM