搜索服务器文件夹并显示存在的文件

时间:2013-12-05 11:45:38

标签: php

我有一个名为uploads的文件夹,在这个文件夹中是文件上传年份的文件夹(2013,2014,2015等...),年份文件夹里面有用户名(rick,dan,edward等... ),usernames文件夹内是文件上传月份的文件夹1-12。

我想搜索文件夹,如果文件夹中有文件显示链接,我遇到的问题是弄清楚如何检查所有文件夹:

到目前为止,这是我的代码:

<?php

            //array for years to check for
            $year = array('2013','2014','2015','2016','2017','2018','2019','2020');
            $month = array('1','2','3','4','5','6','7','8','9','10','11','12');

            //loop through years array and populate $existingYears[] array with years that exist on server
            foreach($year as $years){

                if(is_dir($yearPath = ABSPATH."/"."uploads/".$years."/".$username)){

                    $existingYears[] = $years;

                }

            }

            //loop through existings years
            foreach($existingYears as $year){

                //title
                print "<h1>".$year."</h1>";

                    //loop for months
                    for ($i=1; $i<=12; $i++) {

                        if($i == 1){
                            $title = "January";
                        } elseif($i == 2){
                            $title = "February";
                        }elseif($i == 3){
                            $title = "March";
                        }elseif($i == 4){
                            $title = "April";
                        }elseif($i == 5){
                            $title = "May";
                        }elseif($i == 6){
                            $title = "June";
                        }elseif($i == 7){
                            $title = "July";
                        }elseif($i == 8){
                            $title = "August";
                        }elseif($i == 9){
                            $title = "September";
                        }elseif($i == 10){
                            $title = "October";
                        }elseif($i == 11){
                            $title = "November";
                        }elseif($i == 12){
                            $title = "December";
                        }

                        //path to months directories
                        $url=$userPath.'/'.$i;

                        $newUrl = $url.'/'.$files[2];

                        print $newUrl;

                        //check if directory exists
                        if(is_dir($url)){

                            //assign open state to $dir
                            $dir = opendir($url);

                            //add all files to $files[] array
                            while (($file = readdir($dir)) !== false){

                                $files[] = $file;

                            }
                            closedir($dir);

                            //display link to payslips
                            print "<div class='month-box' id='box$i'>";
                            print "<h2>".$title."</h2>";
                            print "<a class='download-link' id='download-link-$i' href='".$host."/uploads/".$year.'/'.$username.'/'.$i.'/'.$files[2]."'>Download</a>";
                            print "</div>";

                        }

                    }

            }

            ?>

非常凌乱我知道,我确信有更好的方法可以做到这一点,但不确定如何。

2 个答案:

答案 0 :(得分:0)

我不会为你编写代码,但会指出正确的方向。需要考虑的事项: 基于你的斜杠,你似乎在基于* nix的系统上运行,所以

  1. 考虑将unix find与php shell_exec ..
  2. 一起使用
  3. 尝试在查找调用本身中应用过滤器(年,月等)。
  4. 不使用手动命名月份,而是使用php datetime类或简单地使用日期函数

答案 1 :(得分:0)

这将为您提供所有文件和目录的列表(以数组的形式):

$path = 'your/initial/path'
$start_dir = realpath($path);
$directory_arr = array();
$directories = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($start_dir), RecursiveIteratorIterator::SELF_FIRST);
$directories->setFlags(RecursiveDirectoryIterator::SKIP_DOTS); // skips dots '/.' and '/..'
foreach($directories as $directory => $directory) {
   $directory_arr[] = $directory;
}

这将为您提供仅列出目录(交换代码)的列表

if(is_dir($directory)) {
    $directory_arr[] = $directory.'/';
}

这将为您提供仅文件列表(交换代码)

if( ! is_dir($directory)) {
    $file_path_arr[] = $directory;
}