<?php
	define("IN_QKWEB",true);
	$rootPath = str_replace("plugins/editor/xheditor/upload.php","",str_replace("\\","/",__FILE__));
	require($rootPath."includes/init.php");
    error_reporting( E_ERROR | E_WARNING );
    //上传配置
    $config = array(
        "savePath" => ROOT_PATH."data/attachment/images/",
        "maxSize" => 1000 , //单位KB
        "allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"  )
    );
	$save_path =  ROOT_PATH."/data/attachment/images/";
	$year = date("Y",time());
	$save_path .=$year."/";
	if(!file_exists($save_path)){mkdir($save_path);}
	$day = date("md",time());
	$save_path .=$day."/";
	if(!file_exists($save_path)){mkdir($save_path);}
	$url_path = "data/attachment/images/".$year."/".$day."/";
	$config['savePath'] = ROOT_PATH.$url_path;
	//echo $config['uploadPath'];exit;

    //上传图片框中的描述表单名称，
    $title = htmlspecialchars( $_POST[ 'pictitle' ] , ENT_QUOTES );
    //生成上传实例对象并完成上传
    $up = new Uploader( "filedata" , $config );

    /**
     * 得到上传文件所对应的各个参数,数组结构
     * array(
     *     "originalName" => "",   //原始文件名
     *     "name" => "",           //新文件名
     *     "url" => "",            //返回的地址
     *     "size" => "",           //文件大小
     *     "type" => "" ,          //文件类型
     *     "state" => ""           //上传状态，上传成功时必须返回"SUCCESS"
     * )
     */
    $info = $up->getFileInfo();

    /**
     * 向浏览器返回数据json数据
     * {
     *   'url'      :'a.jpg',   //保存后的文件路径
     *   'title'    :'hello',   //文件描述，对图片来说在前端会添加到title属性上
     *   'original' :'b.jpg',   //原始文件名
     *   'state'    :'SUCCESS'  //上传状态，成功时返回SUCCESS,其他任何值将原样返回至图片上传框中
     * }
     */
	 $info["url"] = "/".APP_DIR.$url_path.$info["name"];
   // echo "{'url':'" . $info["url"] . "','title':'" . $title . "','original':'" . $info["originalName"] . "','state':'" . $info["state"] . "'}";
	$msg="{'url':'".$info["url"]."','localname':'".jsonString($title)."','id':'1'}";//id参数固定不变，仅供演示，实际项目中可以是数据库ID
$err='';
echo "{'err':'".jsonString($err)."','msg':".$msg."}";

	function jsonString($str)
{
	return preg_replace("/([\\\\\/'])/",'\\\$1',$str);
}
?>