<?php
class file
{

    public static function upload($file, $location = null)
    { // TODO

        display($file);

        $error = [];

        if ($file['error'] == 0) {

            if ($file['size'] == 0) {
                $error[] = "Empty File Input";
            } else if (file_exists($file)) {
                //$upload = move_uploaded_file($file, $location);
                $upload = 1;

                if ($upload == 0) {
                    $error[] = "Upload Failed";
                }
            }
        } else if ($file['error'] == 4) {
        }

        if (empty($error)) {
            $response["success"] = true;
        } else {
            $response["success"] = false;
            $response["error"] = $error;
        }

        display($response);
        return $response;
    }

    public static function size($path)
    {
        if (file_exists(FILE_ROOT . $path)) {

            $filesize = filesize(FILE_ROOT . $path);

            if ($filesize >= 1073741824) {
                $text = number_format($filesize / 1073741824, 2) . ' GB';
            } elseif ($filesize >= 1048576) {
                $text = number_format($filesize / 1048576, 1) . ' MB';
            } elseif ($filesize >= 1024) {
                $text = number_format($filesize / 1024, 0) . ' KB';
            } elseif ($filesize > 1) {
                $text = $filesize . ' bytes';
            } elseif ($filesize == 1) {
                $text = '1 byte';
            } else {
                $text = '0 bytes';
            }
            return $text;
        } else {
            return false;
        }
    }
}
