du4629 2016-09-28 10:24
浏览 30

在php中循环计数文件两次

I have the following function that searches through a folder, grabs all files and folders not beginning with a "."

$results = [];
function getDirCount($directory, &$results, $friend, $static_path) {
    $i = 0;      
    $entries = scandir($directory);
    foreach ($entries as $item) {
        if (!in_array($item, ['.', '..']) && substr($item, 0, 1) !== '.') {
            $path = $directory . '/' . $item;
            $rel_path = $static_path . '/' . $item;
            if (is_dir($path)) {
                getDirCount($path, $results, $friend, $rel_path);
            } else {
                $i++;
                $pathInfo = pathinfo($path);
                $name = $pathInfo['filename'];
                $type = 'unknown';
                if (!empty($pathInfo['extension'])) {
                    $name .= "." . $pathInfo['extension'];
                    switch (strtolower($pathInfo['extension'])) {
                        case "gif":
                        case "jpg":
                        case "png":
                        case "jpeg":
                        case "bmp":
                        //etc..
                        $type = 'image';
                        break;
                        case "mp4":
                        $type = 'video';
                        break;
                    }
                }
                $tttt = filemtime($path);
                $data = [
                    'name' => $name,
                    'username' => $friend,
                    'path' => $rel_path,
                    'type' => $type,
                    'id' => $i,
                    'time' => date('F d Y h:i A', $tttt)
                ];
                $results[] = $data;
            }
        } 
    }
    return $data;
}

Then I have created a loop, to count all the files.

$friend = strtolower($row['username']);
$directoryToScan = '/absolute/path/to/files/'.$friend;
$rel_path = '/relative/path/to/files/'.$friend;
$tree = getDirCount($directoryToScan, $data, $friend, $rel_path);
for($i=(count($data)-1)-($feed-1);$i>=(count($data)-10)-($feed-1);$i--){
    $c = 0;
    if ($data !== NULL) {
        if(($data[$i]['type']=='image') && ($data[$i]['username'] == $friend)) {
            $c++;
      }
      $photo_count=$c;
    }
}

The problem I am having is, every file is being counted twice and output twice. So if I drop a file in a users directory and check, the count will increase by two. If I drop 5 files, the count is increasing by 10.

I have also tried to move the $c = 0 to before the for statement, but then it seems to indefinitely return 1 even when there are dozens of results. Obviously I am looking to get a correct file count.

UPDATE

Adding more code to help explain what $feed is.

On one page I have the following ajax request.

<script>
    var feed;
    function loadmorefeed() {
        if (feed === undefined || feed === null || feed == '') { feed = 0; }
        feed = feed + 1;
        $.ajax({
            'url': 'stream_feed.php',
            'type': 'get',
            'data': {
                'feed': feed
            },
            'success': function (data) {
                $('.get_stream').append(data);
                return;
            },
            'error': function (request, status, error) {
                return;
            }
        });
    }
    loadmorefeed(); loadmorefeed(); loadmorefeed();
</script>

And then on the page doing the counting I have

$feed=$_GET['feed'];
  • 写回答

1条回答 默认 最新

  • donglankui1263 2016-09-29 09:26
    关注

    Not clear on the $feed and why you want it to be to count the files. To Count all the files you can use the below after function

    //GIVES YOU THE TOTAL COUNT
    $TotaFilesScaned = count($data);
    
    
    //TO GET THE COUNT OF IMAGES ONLY  
    $photo_count = 0;
    for($i=0; $i<$TotaFilesScaned;$i++){
        if(isset($data[$i]) && ($data[$i]['type']=='image') && ($data[$i]['username'] == $friend)) 
        {
                $photo_count++;
        }
    }
    echo $photo_count;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。