<?php
class FileUpload
{
    public static function upload($file, $destinationDir = UPLOAD_DIR)
    {
        if (!isset($file['name']) || $file['error'] !== UPLOAD_ERR_OK) {
            return ["success" => false, "message" => "Invalid file upload."];
        }

        // Create directory if not exists
        if (!file_exists($destinationDir)) {
            mkdir($destinationDir, 0777, true);
        }

        $fileName = basename($file['name']);
        $targetFilePath = $destinationDir . uniqid() . '_' . $fileName;
        $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);

        // Allow certain file formats
        $allowTypes = array('jpg', 'png', 'jpeg', 'gif');
        if (in_array(strtolower($fileType), $allowTypes)) {
            if (move_uploaded_file($file['tmp_name'], $targetFilePath)) {
                return ["success" => true, "filePath" => $targetFilePath];
            } else {
                return ["success" => false, "message" => "File upload failed, please try again."];
            }
        } else {
            return ["success" => false, "message" => "Sorry, only JPG, JPEG, PNG, & GIF files are allowed."];
        }
    }
}
?>