如何读取/更改文件浏览目录?

时间:2012-04-25 20:22:38

标签: php directory

我对如何做到这一点真正脱节 - 部分原因是我没有使用文件系统。我正在尝试创建一个脚本,将脚本下面的文件导航到文件夹。该文件当前的工作原理是列出它所在目录的内容(又名“。”)。我很难理解如何列出下面文件夹的内容以及最好的方法。我的脚本需要如何重新设计才能达到这个目标?

[代码]

// sets ordering variables
$a = $_GET['a'];

// are you allowed to delete?
if($_SESSION['delete'] == "ON") {
    $do_del = true;
    }
else {
    $do_del = false;
    }

// scandir to opendir coversion from stackoverflow.com/questions/6823489/
$files_dir = ".";
$dir_handle = opendir($files_dir);
while ($dir_temp = readdir($dir_handle)) {
    $arr_dir[] = $dir_temp;
    }
closedir($dir_handle);


// function created by "acecream" on php.net; modified to include reverse sort
function sortArray($array, $key, $reverse = false)
    {
    if(isset($array)) {
        foreach ($array as $i => $k)
            { $sort_values[$i] = $array[$i][$key]; }

        if ($reverse == true)
            { arsort ($sort_values); }
        else
            { asort ($sort_values); }
        reset ($sort_values);

        while (list ($arr_key, $arr_val) = each ($sort_values))
            { $sorted_arr[] = $array[$arr_key]; }

        return $sorted_arr;
        }
    }


foreach($arr_dir as $file)
    {
    if(!preg_match("/(_vti)|(_borders)|(_private)|(hidden)|(~_)|(\.php)/", $file) && ($file != ".") && ($file !=".."))
        {
        $full   = "$files_dir/$file";

        $name   = strval($file);
        $size   = sprintf("%u", filesize($full));
        $kind   = pathinfo($file, PATHINFO_EXTENSION); // strval(end(explode(".", $file)));
        $mtime  = filemtime($full); // modify date
        $ctime  = filectime($full); // creation date

        $arr_files[] = array($name, $size, $kind, $mtime, $ctime);
        }
    }
$arr_count = count($arr_files);

switch ($a) {
    case 1 : $arr_files = sortArray($arr_files, 0); break;
    case 2 : $arr_files = sortArray($arr_files, 0, true); break;
    case 3 : $arr_files = sortArray($arr_files, 1); break;
    case 4 : $arr_files = sortArray($arr_files, 1, true); break;
    case 5 : $arr_files = sortArray($arr_files, 2); break;
    case 6 : $arr_files = sortArray($arr_files, 2, true); break;
    case 7 : $arr_files = sortArray($arr_files, 3); break;
    case 8 : $arr_files = sortArray($arr_files, 3, true); break;
    default: $arr_files = sortArray($arr_files, 3, true); break;
    }
unset($a);

function myFileSize($value) {
    // convert raw byte site to friendlier forms
    if($value > 1000)       { $sizestr = round(($value/1024), 1) . " KB"; }
        else                { $sizestr = "$value B"; }
    if($value > 1000000)    { $sizestr = round(($value/1048576), 1) . " MB"; }
    if($value > 1000000000) { $sizestr = round(($value/1073741824), 1) . " GB"; }
    return $sizestr;
    }
function myDate($value) {
    $date = date("j M y G:i", $value);
    return $date;
    }

if($arr_count > 0)
    {
    if($do_del == true) {
        print "<form method=\"post\" action=\"./?d=delete\">\n";
        }
        print "<table>\n";
        print "     <tr>\n";
        print "         <td>File Name</td>\n";
        print "         <td>Size</td>\n";
        print "         <td>Type</td>\n";
        print "         <td>Modified</td>\n";
        print "         <td>Created</td>\n";
    if($do_del == true) {
        print "         <td>Delete?</td>\n";
        }
        print "     </tr>\n";
        print "     <tr style=\"vertical-align: middle; background-color: #fff; border: 1px solid #000;\">\n";
        print "         <td>\n";
        print "             <a href=\"./?a=1\"><img src=\"./~_arr-asc.gif\" border=\"0\"></a>\n";
        print "             <a href=\"./?a=2\"><img src=\"./~_arr-dsc.gif\" border=\"0\"></a>\n";
        print "         </td>\n";
        print "         <td>\n";
        print "             <a href=\"./?a=3\"><img src=\"./~_arr-asc.gif\" border=\"0\"></a>\n";
        print "             <a href=\"./?a=4\"><img src=\"./~_arr-dsc.gif\" border=\"0\"></a>\n";
        print "         </td>\n";                       
        print "         <td>\n";
        print "             <a href=\"./?a=5\"><img src=\"./~_arr-asc.gif\" border=\"0\"></a>\n";
        print "             <a href=\"./?a=6\"><img src=\"./~_arr-dsc.gif\" border=\"0\"></a>\n";
        print "         </td>\n";
        print "         <td>\n";
        print "             <a href=\"./?a=7\"><img src=\"./~_arr-asc.gif\" border=\"0\"></a>\n";
        print "             <a href=\"./?a=8\"><img src=\"./~_arr-dsc.gif\" border=\"0\"></a>\n";
        print "         </td>\n";
        print "         <td>&nbsp;</td>\n";
    if($do_del == true) {
        print "         <td>&nbsp;</td>\n";
        }
        print "     </tr>\n";

    for ($beat = 0; $beat < $arr_count; $beat++)
        {
        $name   = $arr_files[$beat][0];
        $size   = $arr_files[$beat][1];
        $ext    = strtolower($arr_files[$beat][2]);
        $mdate  = $arr_files[$beat][3];
        $cdate  = $arr_files[$beat][4];

        $size   = myFileSize($size);
        $mdate  = myDate($mdate);
        $cdate  = myDate($cdate);

        // pick the image for file type based on file extension
        if(is_dir($name)) {
            $pic = "~_folder.png";
            }
        else {
            $pic = "~_file.png";
            }


        $url  = "<a href=\"$files_dir/$name\">$name</a>";   // create hard link

            print " <tr>\n";
            print "     <td style=\"width: 300px;\"><img src=\"./$pic\"> $url</td>\n";
            print "     <td style=\"width:  75px;\">$size</td>\n";
            print "     <td style=\"width:  50px;\">$ext</td>\n";
            print "     <td style=\"width: 150px;\">$mdate</td>\n";
            print "     <td style=\"width: 150px;\">$cdate</td>\n";
        if($do_del == true) {
            print "     <td><input type=\"checkbox\" name=\"list[$files_dir/$name]\" value=\"ON\"></td>\n";
            }
        print " </tr>\n";
        }
    print "</table>\n";

    if($do_del == true) {
        print " <p><input name=\"Submit1\" type=\"submit\" value=\"submit\">\n";
        print " <input name=\"Reset1\" type=\"reset\" value=\"reset\"></p>\n";
        print "</form>\n";
        }
    }
else
    {
    print "There are no files available for download.";
    }

[/代码]

1 个答案:

答案 0 :(得分:1)

使您的脚本成为一个接收路径的函数。

然后在if语句里面if(is_dir($ file))调用你的函数并传递$ file。

例如,

function myFileSize($value) {...}
function myDate($value) {...}

function list_contents($path)
{
    $dir_name = $path;
    .
    .
    .
    $dir = opendir($path);
    .
    .
    .
    while ($file = readdir($dir)) {
        // do some stuff
        if (is_dir($file)) {
            list_contents($file);
        }
    }
}

根据您的要求,您需要修改list_contents()函数的输出以使用递归。您可以使用数组生成一些树结构,然后打印出树,如:

// return an array representing tree structure
function list_contents($path, &$tree)
{
    // ...
    while ($file = readdir($dir)) {
        $tree[$path][] = $file;
        if (is_dir($file)) {
            list_contents($file, $tree[$path]);
        }
    }

    return $tree;
}

function print_tree($tree)
{
    // code to display array using tree structure
}

$path = '.';
$dir_name = $path;

$tree_array = array();
$tree = list_contents($path, $tree_array)

print_tree($tree);

或者您可以添加一个变量来计算方法中的级别数

function list_contents($path, $level = 0)
{
    //...
    // indent output text by $level number of tabs
    while($file = readdir($dir) {
        if (is_dir($file)) {
            list_contents($file, $level++)
        }
    }
}

list_contents($path);

输出看起来像

> Root
    > Folder1 in root
    > Folder2 in root
        > Folder1 in Folder2
        > Folder2 in Folder2
    > Folder3 in root
        > Folder1 in Folder3
              > Folder1 in Folder1/Folder3
        > File1 in Folder1
    > File1 in root
    > File2 in root

作为一个建议,我会采取任何与获取文件信息或操作文件相关的函数,并将它们放在自己的文件file_functions.php中,然后将其包含在脚本的顶部。