<?php
require '../connForms/acdet.php';

$path = "";

$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

$sqlUsers = "SELECT username FROM accounts WHERE streamstatus = 'streamoff'";
$result = $mysqli->query($sqlUsers);

$usernames = array();

if ($result->num_rows > 0) {
    $i = 0;
    //while loop passes through each row from results and puts each username into unsernames array
    while($row =  $result->fetch_assoc()) {
        $name = $row['username'];
        $usernames[$i] = $row['username'];
        $i++; 
    }
    
}

//creats dashCam folder path for each user and calls delAllImg function
foreach ($usernames as $name) {
    $path = "../datBundle/".$name."/dashCam/";
    delAllImg($path);
}

//delets all images from dashCam folder for a user
function delAllImg($path) {
    //gets all images in the forlder to an array and eliminates . & .. entires
    $files = array_diff(scandir($path), array('.', '..'));

    //delets each image it finds
    foreach($files as $file) {
        $del = $path.$file;
        if(is_file($del)) {
            //echo $del;
            unlink($del); // delete file
        }
        
    }

}

?>