<?php
/**
 * Created by PhpStorm.
 * User: jessehu
 * Date: 16/10/5
 * Time: 下午3:59
 */
define('YUKEY', true);
require(dirname(__FILE__) . '/../includes/init.php');
$upfield = $_REQUEST['name'];
$uploadFiles = $_FILES[$upfield];
//var_dump($upfield);exit;
$acceptFileType = $_REQUEST['filetype'];
$maxFileSize = $_REQUEST['maxsize'];
$path = $_REQUEST['path'];
$result = array('state'=>true, 'errmsg'=>'', 'data'=>array());
if(is_array($uploadFiles) && !empty($uploadFiles)){
    //$tmpDir = ROOT_PATH.'../images/upload/image/temp';
	  switch ($upfield)
		{
		case 'appearance_img':
		 	  $tmpDir = ROOT_PATH.'upload/files/' . $path . '/defects';
		  break;
		case 'style_img':
		  	  $tmpDir = ROOT_PATH.'upload/files/' . $path . '/styleimg';
		  break;
		case 'pack_img':
		  	  $tmpDir = ROOT_PATH.'upload/files/' . $path . '/packimg';
		  break;
		case 'test_img':
		  	  $tmpDir = ROOT_PATH.'upload/files/' . $path . '/testimg';
		  break;
		default:
		  echo "No this kind of pic save path!";
		}
    foreach($uploadFiles['error'] as $k=>$v){
        if($uploadFiles['error'][$k] > 0){
            $result['state'] = false;
            $result['errmsg'] = '上传文件有误,请重试!';
            break;
        }else{
            $sourceFileName = $uploadFiles['name'][$k];
            $sourceFileExt = strrchr($sourceFileName, '.');
            $fileName = date('YmdHis').'_'.mt_rand(10000,999999).$sourceFileExt;
            if(move_upload_file($uploadFiles['tmp_name'][$k], $tmpDir.'/'.$fileName)){
                $result['data'][$k] = array();
                $result['data'][$k]['sourceFileName'] = $sourceFileName;
                $result['data'][$k]['realFileName'] = $fileName;
            }
        }
    }
}else{
    $result['state'] = false;
    $result['errmsg'] = '上传文件有误,请重试!';
}

exit(json_encode($result));