<?php
include("require.php");
if($core_file=="1" && !isset($_COOKIE["core_user_name"]) || $core_file=="0")
  Header("Location: error.php?id=2");
elseif($_GET["action"]=="upload")
{
  $oldname=$_FILES["file"]["name"];
  $fileextensionname=strtolower(eregi_replace(".+(\..+)","\\1",$oldname));
  $filename = time().random(4).$fileextensionname;
  if($_FILES["file"]["error"]>0)
  {
    ?>
	  <div style="width:70px; float:left; text-align:left; font-size:12px;">上传附件:</div>
	  <div id="file" style="float:left; text-align:left; padding:2px; font-size:12px;">上传失败 <a href="upload.php">重新上传</a></div>
	<?php
  }
  elseif(!eregi($core_filetype,$fileextensionname))
  {
    ?>
	  <div style="width:70px; float:left; text-align:left; font-size:12px;">上传附件:</div>
	  <div id="file" style="float:left; text-align:left; padding:2px; font-size:12px;">上传的文件类型不被容许 <a href="upload.php">重新上传</a></div>
	<?php
  }
  elseif(($_FILES["file"]["size"]/1024)>$core_filesize)
  {
    ?>
	  <div style="width:70px; float:left; text-align:left; font-size:12px;">上传附件:</div>
	  <div id="file" style="float:left; text-align:left; padding:2px; font-size:12px;">上传的文件大小超过限制 <a href="upload.php">重新上传</a></div>
	<?php
  }
  else
  {
    $directory=date('Y-m');
	if(!file_exists('upload/'.$directory))
	  mkdir("upload/".$directory, 0777);
	move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$directory."/".$filename);
	$username=$_SERVER['REMOTE_ADDR'];
	if(isset($_COOKIE["core_user_name"]))
	  $username=$_COOKIE["core_user_name"];
	$time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
	$type=1;
	if(!preg_match( '/^image\//i ',$_FILES["file"]["type"])) 
      $type=2;
	$phpcolor->table=$core_mysql_prefix."upload";
    $phpcolor->field="oldfilename,filename,filesize,filetype,username,uptime,type";
	$phpcolor->where="'".$oldname."','".$directory."/".$filename."','".$_FILES["file"]["size"]."','".$type."','".$username."','".$time."','0'";
    $phpcolor->write();
	$phpcolor->field="id";
	$phpcolor->where="filename = '".$directory."/".$filename."'";
	$file=$phpcolor->exist(1);
	echo "<input type=hidden value=".$file['id']." id=imgurl>";
	echo "<script>if (parent.document.getElementById('file').value==''){parent.document.getElementById('file').value=document.getElementById('imgurl').value;}else{parent.document.getElementById('file').value=parent.document.getElementById('file').value+'|'+document.getElementById('imgurl').value;}</script>";
	echo "<script>parent.document.getElementById('filelbl').style.display=''</script>";
	echo "<script>parent.document.getElementById('filelb').style.display=''</script>";
	echo "<script>parent.document.getElementById('filelbl').innerHTML=parent.document.getElementById('filelbl').innerHTML+'<br />'</script>";
	echo "<script>if (parent.document.getElementById('filelb').innerHTML==''){parent.document.getElementById('filelb').innerHTML='<div>".$oldname." (附件大小：".(round($_FILES["file"]["size"]/1024/1024, 2))."MB)</div>';}else{parent.document.getElementById('filelb').innerHTML=parent.document.getElementById('filelb').innerHTML+'<div>".$oldname." (附件大小：".(round($_FILES["file"]["size"]/1024/1024, 2))."MB)</div>'}</script>";
	?>
	  <div style="width:70px; float:left; text-align:left; font-size:12px;">上传附件:</div>
	  <div id="file" style="float:left; text-align:left; padding:2px; font-size:12px;">附件上传成功 <a href="upload.php">继续上传</a></div>
	<?php	
  }
}
else
{
?>
<form action="upload.php?action=upload" method="post" enctype="multipart/form-data">
<div style="width:70px; float:left; text-align:left; font-size:12px;">上传附件:</div>
<div style="float:left;" id="file"><input type="file" name="file" style="width:370px; height:20px; border:#ABC8EA 1px solid;"/>&nbsp;<input type="submit" value="上传" /></div>
</form>
<?php
}
?> 