Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 搜索服务器文件夹并显示存在的文件_Php - Fatal编程技术网

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

Php 搜索服务器文件夹并显示存在的文件,php,Php,我有一个名为uploads的文件夹,该文件夹是文件上载年份的文件夹(201320142015等),该年度文件夹中有用户名(rick、dan、edward等),用户名文件夹中有文件上载月份的文件夹1-12 我想搜索文件夹,如果文件夹中有文件显示链接,我遇到的问题是如何检查所有文件夹: 以下是我目前的代码: <?php //array for years to check for $year = array('2013','2014','201

我有一个名为uploads的文件夹,该文件夹是文件上载年份的文件夹(201320142015等),该年度文件夹中有用户名(rick、dan、edward等),用户名文件夹中有文件上载月份的文件夹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>";

                        }

                    }

            }

            ?>

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

  • 考虑将unix
    find
    与php
    shell\u exec
    一起使用
  • 尝试在find调用本身中应用过滤器(年、月等)
  • 使用php datetime类或仅使用date函数,而不是手动命名月份

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

    $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;
    }
    
    这将为您提供一个目录列表(交换代码)

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


    这个问题似乎与主题无关,因为它更适合codereview.stackexchange.com谢谢,我不知道现有的应用程序需要在所有类型的系统上工作,所以不想将代码限制在一个系统上。您的“斜杠”与windows不兼容。你想先改变一下。
    if(is_dir($directory)) {
        $directory_arr[] = $directory.'/';
    }
    
    if( ! is_dir($directory)) {
        $file_path_arr[] = $directory;
    }