<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<?php  

require_once '../inc/common.inc.php';
$fileMaxSize = $kuWebsiteUploadFileMax*1024*1024;
$fileSize = $_FILES['imgurl']['size'];
if('' != $fileMaxSize && $fileSize > $fileMaxSize)
{
  ?>
  <script type="text/javascript">
  alert(<?php echo '\''.$promptIncludeDirCorrectUploadFileSizeFormat.'\''; ?>);
  window.location.href='uploadfile.php?returnid=<?php echo $returnid; ?>';
  </script>
  <?php  
  exit;
}

function upload($form, $fileFormat) 
{
  global $promptIncludeDirUploadFileCanNotWrite, $promptIncludeDirCorrectUploadFileFormat, $promptIncludeDirCopyUploadFileError;
  if (is_array($form)) 
  {
    $filear = $form;
  } 
  else 
  {
    $filear = $_FILES[$form];
  }
  if (!is_writable('../../upload/file/')) 
  {
     ?>
    <script type="text/javascript">
      alert('\''."<?php echo $promptIncludeDirUploadFileCanNotWrite; ?>".'\'');
      window.location.href='uploadfile.php?returnid=<?php echo $returnid; ?>';
    </script>
    <?php  
    exit;
  }
  
  $ext = explode(".", $filear["name"]);
  $ext = $ext[1];
  
  srand((double)microtime() * 1000000);
  $rnd = rand(100, 999);
  $name = date('U') + $rnd;
  $name = $name.".".$ext;
  if ($fileFormat != "" && !in_array(strtolower($ext), explode("|", strtolower($fileFormat)))) 
  {
    echo "<script>alert(".'\''.$promptIncludeDirCorrectUploadFileFormat.'\''.");window.location.href='uploadfile.php';</script>";
    exit;
  }
  if (!copy($filear["tmp_name"], "../../upload/file/".$name)) 
  {
    echo "<script>alert(".'\''.$promptIncludeDirCopyUploadFileError.'\''.");window.location.href='uploadfile.php';</script>";
    exit;
  } 
  else 
  {
    @unlink($filear["tmp_name"]); 
  }
  return "../upload/file/".$name;
}

$downloadurl=upload('imgurl', $kuWebsiteAllowUploadFileFormat);
if($returnid == "")$returnid="fileurl";  
echo "<SCRIPT language=javascript>\n";
echo "parent.document.myform.".$returnid.".value='".$downloadurl."';\n";
if($uploadtype == "download" )
{ 
  echo "parent.document.myform.filesize.value='".round($fileSize/1024,2)."';\n";
}
echo "alert(\"$promptIncludeDirUploadFileRealSizeSuccess";
echo round($fileSize/1024,2);
echo "K\");";
echo "location.href='uploadfile.php?returnid=$returnid'; </script>";  
 
?>
</body>
</html>