使用PHP列出文件并开始一个新的行
问题描述:
我想列出文件夹中的文件并将它们放入列表中。 该列表应该拆分每个3个文件。该列表将是Materialise中的卡片, 因此,为什么我想为每个3个文件开始一个新行。 我会如何实现这样的事情? 有人可以帮我吗? 谢谢:)使用PHP列出文件并开始一个新的行
答
<?php
$myDirectory = opendir("./downloads");
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
closedir($myDirectory);
// count elements in array
$indexCount = count($dirArray);
// sort 'em
sort($dirArray);
// loop through the array of files and print them all
for($index=0; $index < $indexCount; $index++) {
if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
print('<li class="collection-item dismissable"><div>'.$dirArray[$index].'<a href="https://DOMAIN.xyz/downloads/'.$dirArray[$index].'" class="secondary-content"><i class="material-icons">send</i></a></div></li>');
}
}
?>
答
您应该使用DirectoryIterator与PHP5.6 + 那来看看下面的
$dir = "somepath";
$files = Array(Array(), Array(), Array()); // This should set up 3 nested arrays
$ind = 0;
foreach(new DirectoryIterator($dir) as $fileInfo) {
if ($fileInfo->isDot()) continue;
if ($ind >= 3) $ind = 0;
array_push($files[$ind], $fileInfo->getFileName());
$ind++;
}
+0
得到一些错误。 –
+0
忘记了一些语法糖。现在它工作,玩得开心 – Fohlen
你好,你可以告诉你已经尝试的代码? – webNeat
'if($ counter == 3){echo'somethig'; }' –