<?

function file_uploadfile($puserfile,$ptempfile,$uploaddir) {
  // note: $puserfile for $_FILES['<inputname>']['name']
  //       $ptempfile for $_FILES['<inputname>']['tmp_name']
  global $db;
  // $uploaddir = 'public/';
  if (($puserfile!=NULL)&&($puserfile!="")) {
    // get filenames and extension..
    $wordarray = explode("/",$puserfile);
    $lastword = count($wordarray) - 1;
    $originalname = $wordarray[$lastword];
    $namearray = explode(".",$originalname);
    $lastname = count($namearray) - 1;
    $fileext = $namearray[$lastname];
    $newfilename = $namearray[$lastname-1].".".$fileext;
    $uploadfile = $uploaddir.$newfilename;
    // upload (lib_general)..
    $tmpuploadfile = $ptempfile;
    $resultupload = uploadFile($tmpuploadfile, $uploadfile);
    if($resultupload==TRUE) {
      return $newfilename;
    } else {
      return "";
    }
  } else {
    return "";
  }
}

function file_uploadmaterial($puserfile,$ptempfile,$uploaddir) {
  // note: $puserfile for $_FILES['<inputname>']['name']
  //       $ptempfile for $_FILES['<inputname>']['tmp_name']
  // upload dir is pkey!
  global $db;
  mkdir('assignment/'.$uploaddir);
  $uploaddir = 'assignment/'.$uploaddir.'/materials';
  mkdir($uploaddir);
  if (($puserfile!=NULL)&&($puserfile!="")) {
    // get filenames and extension..
    $wordarray = explode("/",$puserfile);
    $lastword = count($wordarray) - 1;
    $originalname = $wordarray[$lastword];
    $namearray = explode(".",$originalname);
    $lastname = count($namearray) - 1;
    $fileext = $namearray[$lastname];
    $newfilename = $namearray[$lastname-1].".".$fileext;
    $uploadfile = $uploaddir.'/'.$newfilename;
    // upload (lib_general)..
    $tmpuploadfile = $ptempfile;
    $resultupload = uploadFile($tmpuploadfile, $uploadfile);
    if($resultupload==TRUE) {
      return $newfilename;
    } else {
      return "";
    }
  } else {
    return "";
  }
}

function file_uploadsubmission($puserfile,$ptempfile,$uploaddir,$uploadauthor) {
  // note: $puserfile for $_FILES['<inputname>']['name']
  //       $ptempfile for $_FILES['<inputname>']['tmp_name']
  // upload dir is pkey!
  global $db;
  mkdir('assignment/'.$uploaddir);
  mkdir('assignment/'.$uploaddir.'/submissions');
  $uploaddir = 'assignment/'.$uploaddir.'/submissions/'.$uploadauthor;
  mkdir($uploaddir);
  if (($puserfile!=NULL)&&($puserfile!="")) {
    // get filenames and extension..
    $wordarray = explode("/",$puserfile);
    $lastword = count($wordarray) - 1;
    $originalname = $wordarray[$lastword];
    $namearray = explode(".",$originalname);
    $lastname = count($namearray) - 1;
    $fileext = $namearray[$lastname];
    $newfilename = $namearray[$lastname-1].".".$fileext;
    $uploadfile = $uploaddir.'/'.$newfilename;
    // upload (lib_general)..
    $tmpuploadfile = $ptempfile;
    $resultupload = uploadFile($tmpuploadfile, $uploadfile);
    if($resultupload==TRUE) {
      return $newfilename;
    } else {
      return "";
    }
  } else {
    return "";
  }
}

?>