简体   繁体   English

PHP在多个目录中输出多个文件

[英]PHP Output Multiple Files In Multiple Directories

I have a main directory called PDFS that has 5 sub directories representing years(2011, 2012, 2013, 2014, 2015). 我有一个名为PDFS的主目录,其中有5个子目录,分别代表年份(2011、2012、2013、2014、2015)。 In each "year directory" are 11 pdfs. 每个“年目录”中有11个pdf。 I need to access the PDFS directory and get all "year directories", and then get all pdfs in each "year directory" to display on a page. 我需要访问PDFS目录并获取所有“年份目录”,然后获取每个“年份目录”中的所有pdf才能显示在页面上。 So for example the pdfs in the 2011 "year directory" will be listed under a 2011 heading. 因此,例如2011年“年目录”中的pdf将列在2011年标题下。 Same with 2012, 2013 etc. I've tried many different things. 与2012、2013等相同。我尝试了许多不同的方法。 I can output the "year directories" using scandir(). 我可以使用scandir()输出“年份目录”。 I can output the files in a single "year directory" using a foreach loop and glob(). 我可以使用foreach循环和glob()在单个“年份目录”中输出文件。 I can't for the life of me figure out how to output each "year directory's" files other than doing multiple foreach loops using glob() on each "year directory". 除了使用每个“年份目录”上的glob()进行多个foreach循环外,我一生都无法弄清楚如何输出每个“年份目录”的文件。 There's gotta be a more dynamic way to set this up. 必须有一种更动态的方式来进行设置。 Any suggestions? 有什么建议么? I feel like I would need some kind of foreach inside of a foreach? 我觉得我在foreach中需要某种foreach吗? Here's what I'm working with: 这是我正在使用的:

//set main directory
$dir = 'PDFS';

//gets sub directories of PDFS directory
$directories = scandir($dir);

//removes the first to indexes in the directories array that are just dots
unset($directories[0]);
unset($directories[1]);

//outputs an array of the sub directories from scandir() starting at index 2
print_r($directories);

//initialize $i variable for loop
$i=0;

//if I manually set the $dir variable to "PDFS/2011/*" this will output links
//to each pdf inside the 2011 year directory
    foreach(glob($dir) as $file) {
        $i++;
        echo "<a href='$file'>$file</a><br />";
    }

You were on the right track, this piece of code should do the trick: 您处在正确的轨道上,这段代码应该可以解决问题:

//set main directory
$mainDir = 'PDFS';

//gets sub directories of PDFS directory
$subDirectories = scandir($mainDir);

//removes the first two indexes in the directories array that are just dots
unset($subDirectories[0]);
unset($subDirectories[1]);

// first loop through all sub directories ...
foreach($subDirectories as $subDirectory) {
    echo '<h1>'.$subDirectory.'</h1>';
    // ... and then loop through all files in each sub directory
    foreach(glob($mainDir.'/'.$subDirectory.'/*') as $file) {
        echo "<a href='$file'>$file</a><br />";
    }
}

I changed some of the var names for better understanding. 为了更好地理解,我更改了一些var名称。

A few things: 一些东西:

  • There is no need for the counting var i as you use a foreach. 使用foreach时无需计算变量i

  • If you set a string together manually, then try to generate it step-by-step and just compare the generated with the manual one until it fits. 如果您将字符串手动设置在一起,则尝试逐步生成它,然后将生成的字符串与手动的字符串进行比较,直到适合为止。 (You connect two strings with a dot . ) (您用点将两个字符串连接起来.

  • You can configure the glob function, so that it only finds certain file types ( *.pdf ). 您可以配置glob函数,使其仅查找某些文件类型( *.pdf )。

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

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