<?php

$userName = $_POST['userFolder'];
$path = "../datBundle/".$userName."/dashCam/";


function allFiles() {
    //$path    = "user1/";
    global $path;
    $files = scandir($path);
    //the line below clears . and .. from the first two positions
    //but the array still starts from 2 index that's why we use the for loop
    //$files = array_diff(scandir($path), array('.', '..'));
    $arr = [];
    $j = 0;
    for ($i = 2; $i < count($files); $i++) {
        $arr[$j] = $files[$i];
        $j++;
    }
    return $arr;
}

$imgs = allFiles();
$firstIMG = "";

for ($i = 0; $i < count($imgs); $i++) {
    if ($i == 0) {
        $firstIMG = $imgs[$i]."-";       
    }
    if ($i == 1) {
        $firstIMG = $firstIMG.$imgs[$i];
    }
    if ($i > 1) {
        break;
    }
}

$firstIMG = $firstIMG."-".count($imgs);

//$firstIMG = $imgs[0];
echo $firstIMG;
?>