dtyz76562 2019-03-04 11:33
浏览 33

列出目录但不是子目录中的文件PHP [重复]

This question already has an answer here:

I have the following code that I am using to list all the files in a specific directory which I then return as JSON

<?php
header('Content-Type: application/json');
$vars = filter_input_array( INPUT_POST, FILTER_SANITIZE_STRING );

$id = $vars['id'];

$dir = $_SERVER['DOCUMENT_ROOT'] . '/resources/' . $id;

$list = array(); //main array

if(is_dir($dir)){
if($dh = opendir($dir)){
    while(($file = readdir($dh)) != false){

        if($file == "." or $file == ".."){
            //...
        } else { //create object with two fields

            array_push($list, $file);
        }
    }
}


echo json_encode($list);
}
?>

However, as well as returning the files, it is also returning a subdirectory that is also in that folder. How do change the if statement to also check whether it is an actual file that has been retrieved?

</div>
  • 写回答

1条回答 默认 最新

  • douchilian1009 2019-03-04 11:35
    关注

    Meet is_dir()

    if ($file == "." or $file == ".." or is_dir($file)) {
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题