显示文件但不显示目录

时间:2014-10-22 04:22:38

标签: php directory scandir

所以这段代码应该从目录中列出文件。但它也显示了目录..我想删除它.​​.使用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>

4 个答案:

答案 0 :(得分:0)

使用is_file功能,您可以检查当前项目是否为文件。

试试此代码

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

答案 1 :(得分:0)

您应该在foreach声明中使用is_file()

喜欢如此:

$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
  }
}

答案 2 :(得分: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
       }
    }
} 

答案 3 :(得分: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>
相关问题