<?php
include_once("CUserSession.php");
include_once("CProjectDocument.php");
include_once("CUtil.php");
$ds          = DIRECTORY_SEPARATOR;
 
$storeFolder = 'uploads';
 
if (!empty($_FILES)) {
    
    if(isset($_POST["fileType"]) && isset($_POST["projectId"]) && isLoggedIn()){
        $storeFolder = dirname( __FILE__ ) . $ds . $storeFolder . $ds . $_SESSION["user"]->_id . $ds . $_POST['projectId'];
        $ret = [];
        $fileType = explode(":", $_POST["fileType"])[0];
        if($fileType == "document"){
            $tempFile = $_FILES['file']['tmp_name'];
            $visibleName = isset($_POST["rename"]) ? $_POST["rename"] : $_FILES['file']['name'];    
        } else {
            $tempFile = $_FILES['file']['tmp_name'][0];
            $visibleName = isset($_POST["rename"]) ? $_POST["rename"] : $_FILES['file']['name'];
        }
        $ret[] = addProjectDocument_DB($_POST["projectId"], $_POST['randString'], $storeFolder, $visibleName, $_POST["fileType"], $tempFile, $_POST["file_desc"]);
    }

    var_dump($ret);
}
?>