алфавитный — бесконечный список каталогов Переполнение стека

У меня есть этот php-код, чтобы перечислить все файлы в каталоге и размер выходного файла и ссылку для скачивания.

<?
function human_filesize($bytes, $decimals = 2) {
$size = array(' B',' kB',' MB',' GB',' TB',' PB',' EB',' ZB',' YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}

$excludedFiles = array('.','..');

$excludedExtensions = array ('html','htm','php');

// Convert to lower case so we are not case-sensitive
for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);


// Define the full path to your folder from root
$dir = "./";

// Open the folder
$dir_handle = @opendir($dir) or die("Unable to open $dir");

// Loop through the files
while ($file = readdir($dir_handle)) {

$extn = explode('.',$file);
$extn = array_pop($extn);

if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)){

if($file == "." || $file == ".." )


continue;

echo "<tr>
<td>
<a class='Testo' href=\"$file\" download>$file</a></td>
<td><font class='TestoPiccoloBo'>[" . human_filesize(filesize($file)) . "]</font></td>
</tr>";
}
}

// Close
closedir($dir_handle);



?>

я хотел, чтобы список был в алфавитном порядке, поэтому я добавил

$files = scandir($dir);

после $dir линия и

foreach ($files as $file){

после while ($file = readdir($dir_handle)) { линия

и

}

перед closedir($dir_handle); линия

Теперь список файлов в алфавитном порядке, но список бесконечен. список начинается снова и снова, как цикл.
Что я делаю неправильно? Это правильный способ сделать это?
Любая помощь приветствуется.
Спасибо!

0

Решение

Вы можете поместить вашу папку в массив и отсортировать ее с помощью функции сортировки php. Затем напечатайте их:

<?
function human_filesize($bytes, $decimals = 2) {
$size = array(' B',' kB',' MB',' GB',' TB',' PB',' EB',' ZB',' YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}

$excludedFiles = array('.','..');
$arrayFiles = array();
$excludedExtensions = array ('html','htm','php');

// Convert to lower case so we are not case-sensitive
for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);


// Define the full path to your folder from root
$dir = "./";

// Open the folder
$dir_handle = @opendir($dir) or die("Unable to open $dir");

// Loop through the files
while ($file = readdir($dir_handle)) {

$extn = explode('.',$file);
$extn = array_pop($extn);

if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)){

if($file == "." || $file == ".." )
continue;

$arrayFiles[] =  $file;

}
} // dunno what are these } so i put my loop after

// Close
closedir($dir_handle);

sort($arrayFiles);

foreach ($arrayFiles as $file) {
echo "<tr>
<td>
<a class='Testo' href=\"$file\" download>$file</a></td>
<td><font class='TestoPiccoloBo'>[" . human_filesize(filesize($file)) . "]</font></td>
</tr>";
}
?>
1

Другие решения

Других решений пока нет …