Файл показывается но не каталоги

так что этот код делает то, что он должен перечислить файлы из каталога .. но он также показывает каталог .. я хочу удалить его .. неправильно использует scandir?

    <ul>
<?php echo "List of files"?>
<?php
$dir = 'kcfinder/upload/files';
$files = scandir($dir);

foreach($files as $ind_file){
?>

<li><?php echo $ind_file;?> </a> | <a href="includes/delete.php?file=<?=$ind_file?>">Delete</a></li>
<?php
}
?>
</ul>

0

Решение

С помощью is_file функция, вы можете проверить, является ли текущий элемент файлом или нет.

Попробуйте этот код

if(is_file($dir . $ind_file)) {
// You code
}
0

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

использование if(is_dir($ind_file)) для получения каталога и if (is_file($ind_file)) для файлов

$full_path = "kcfinder/upload/files";

if ($handle = opendir("$full_path"))
{
while (false !== ($file = readdir($handle)))
{
if(is_dir($full_path."/".$file))
{
if($file!='..' && $file!='.')
{
//for folders
}
}else
{
//for files
}
}
}
0

<ul>
<?php echo "List of files"?>
<?php
$dir = 'kcfinder/upload/files';
$files = scandir($dir);

foreach($files as $ind_file=>$afile){
if($ind_file!=0 & $ind_file!=1){ // skip '.' and '..'
?>
<li><?php echo $afile;?> </a> | <a href="includes/delete.php?file=<?=$afile?>">Delete</a></li>
<?php
}
}
?>
</ul>
0

Вы должны использовать is_file() внутри вашего foreach заявление.

Вот так:

$dir = 'kcfinder/upload/files';
$files = scandir($dir);

foreach ($files as $ind_file) {
if (is_file(__DIR__.'/'.$ind_file)) {
?>
<li><?php echo $ind_file;?> </a> | <a href="includes/delete.php?file=<?=$ind_file?>">Delete</a></li>
<?php
}
}
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector