<?php
set_time_limit(3000);
$session_name=session_name();
if(!isset($_POST[$session_name])){
    exit;
}else{
    session_id($_POST[$session_name]);
    session_start();
}

include_once "../lib/watermark.class.php";

$filename = GetRandID(extend($_FILES["Filedata"]["name"]));
$type = $_FILES["Filedata"]["type"];
$name = $_FILES["Filedata"]["name"];
$ext = ".".extend($_FILES["Filedata"]["name"]);
$size = $_FILES["Filedata"]["size"];
$error = $_FILES["Filedata"]["error"];
$tmp_name  = $_FILES["Filedata"]["tmp_name"];

$uploadType = $_GET["upType"];
switch( $uploadType ){
	case "thumbUpload":
		$upload_dir = "../uploads/thumb/";
	break;
	case "attachmentsUpload":
		$upload_dir = "../uploads/attachments/";
	break;
	case "otherUpload":
		$upload_dir = "../uploads/a/";
	break;
	default:
		$upload_dir = "../uploads/a/";
	break;
}

if(!empty($_SESSION["folder"]))
	$upload_dir .= $_SESSION["folder"]."/";

$picw = $_POST["picw"];
uploadfile($type,$name,$ext,$size,$error,$tmp_name,$filename,$upload_dir,$picw);

function uploadfile($type,$name,$ext,$size,$error,$tmp_name,$targetname,$upload_dir,$picw){
    $MAX_SIZE = 50000000;
    $FILE_MIMES = array('image/pjpeg','image/jpeg','image/jpg','image/gif','image/png','application/octet-stream','application/msword','application/msword','application/vnd.ms-excel','application/vnd.ms-excel','application/vnd.ms-powerpoint','application/vnd.ms-powerpoint','application/zip','application/octet-stream','application/pdf');
    $FILE_EXTS = array('.jpeg','.jpg','.jpg','.gif','.png','.flv','.doc','.docx','.xls','.xlsx','.ppt','.pptx','.zip','.rar','.pdf');

    $file_path = $upload_dir.$targetname;
    
    if(!is_dir($upload_dir)){
        if(!mkdir($upload_dir,0755,true))
            die("文件上传目录不存在并且无法创建文件上传目录");
        if(!chmod($upload_dir,0755,true))
            die("文件上传目录的权限无法设定为可读可写");
    }
    
    if($size > $MAX_SIZE)
        die("上传的文件大小超过了规定大小,最大为50M <a href='javascript:history.back(-1);'>返回>></a>");

    if($size == 0)
        die("请选择上传的文件 <a href='javascript:history.back(-1);'>返回>></a>");
	
    if(!in_array(strtolower($ext),$FILE_EXTS))
        die("请上传符合要求的文件类型jpg/gif/png  <a href='javascript:history.back(-1);'>返回>></a>");

    if(!move_uploaded_file($tmp_name, $file_path))
        die("复制文件失败，请重新上传 <a href='javascript:history.back(-1);'>返回>></a>");
	
	switch($error){
        case 0:
			if(!empty($picw))
				imageZoom($file_path,$picw);
			else
				imageZoom($file_path);
			echo "{\"FileUrl\":\"" . substr($file_path,2) . "\"}";
            exit;
        case 1:
            die("上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值");
        case 2:
            die("上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值");
        case 3:
            die("文件只有部分被上传");
        case 4:
            die("没有文件被上传");
    }	
}

function GetRandID($prefix){
     $seedstr = explode(" ",microtime(),5);
     $seed = $seedstr[0] * 10000;
     srand($seed);
     $random = rand(1,10000);
     $filename = date("YmdHis", time()).$random.'.'.$prefix;
     return $filename;
}

function extend($file_name){
	$extend = explode("." , $file_name);
	$va = count($extend) - 1;
	return $extend[$va];
}
?>