dswm97353 2015-10-23 06:41
浏览 41
已采纳

ajax php检查我在路径src中有多少个文件夹

<?php
header("content-type: application/json");
$files = array();
$dir = "Img/House"; //folder src path

$dirHandle = opendir($dir); 

while(($file = readdir($dirHandle) !== false)){
    if ($file !== "." && $file !== "..")
    {
        $files[] = $file;
    }
}

echo($files);
//echo json_encode($directoryfiles);
?>

I am using ajax to php to return how many folder I had inside that src path , I can count the folder number on ajax , but something wrong with my php file , it seem wont check how many folder I have.

My intention is to use ajax and php check how many folder i have and push those name into the array $files. Can anyone help me take a look. I have no experience one this.

  • 写回答

1条回答 默认 最新

  • dsgd5756 2015-10-23 06:55
    关注

    If you only want to return the number of directories in the given path, you can easily use count and glob, see below

    // this is not needed unless you output json
    // header("content-type: application/json");
    
    $dir = "Img/House"; //folder src path
    
    $dirs = glob($dir . "/*",GLOB_ONLYDIR);
    
    print count($dirs);
    
    // or directly
    // print count(glob($dir . "/*",GLOB_ONLYDIR));
    
    // if glob returns the current and parent dirs, "." and ".."
    // just remove 2 from the count
    // test by doing
    print_r($dirs);
    // then
    print $count($dirs)-2;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本