在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'];

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;