<?php

class utilsDir
{
    function frontendTables($id_user)
    {
        $arrayFiles = $this->getFiles($_SESSION['path_build'] . $id_user);
        $table = "";
        foreach ($arrayFiles as $path) {
            $nameFile = basename($path, ".apk");
            $table = $table . "<tr>
                                <td>$nameFile.apk</td>
                                <td><a href='getfile.php?download=" . base64_encode($nameFile) . "&id=" . $id_user . "'>Download</a></td>
                             </tr>";
        }

        return $table;
    }

    function file_download($file)
    {
        if (file_exists($file)) {
            if (ob_get_level()) {
                ob_end_clean();
            }

            if (false !== ($handler = fopen($file, 'r'))) {
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename=' . basename($file));
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file)); //Remove

                echo file_get_contents($file);
            }
            exit;
        }
    }

    function getFiles($path)
    {
        $dir = opendir($path);
        $array = array();
        $var = 0;
        while ($file = readdir($dir)) {
            if (!is_dir("$path/$file") && $file != '.' && $file != '..') {
                $array[$var] = "$path/$file";
                $var++;
            }
        }
        return $array;
    }
}
