<?php
// +----------------------------------------------------------------------
// | yershop网店管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | 版权申明：yershop网店管理系统不是一个自由软件，是贝云网络官方推出的商业源码，严禁在未经许可的情况下
// | 拷贝、复制、传播、使用yershop网店管理系统的任意代码，如有违反，请立即删除，否则您将面临承担相应
// | 法律责任的风险。如果需要取得官方授权，请联系官方http://www.yershop.com
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Config;
use think\Upload;
class Uploads extends Home{



public function ke_upimg(){
		$name=input("name")?input("name"):"imgFile";
	   //dump($);
		if ((($_FILES[$name]["type"] == "image/gif")
		|| ($_FILES[$name]["type"] == "image/jpeg")
		|| ($_FILES[$name]["type"] == "image/pjpeg")|| ($_FILES[$name]["type"] == "image/png"))
		){
		   $model="picture";//图片文件
		   $data["type"]=1;
		}
		else if(($_FILES[$name]["type"] == "video/mp4")){
		   $model="media";//媒体文件
		   $data["type"]=2;
		}
		else{
		   $model="attachment";//压缩文件等
		   $data["type"]=3;
		}
		$file = request()->file($name);
        //if(!$file){
            //$this->error('参数错误！');
        //}
      // 移动到框架应用根目录/uploads/ 目录下
        $info =$file->validate(['size'=>300*1024*1024,'ext'=>'zip,rar,mp4,MP4,mp3,flv,jpg,png,gif,doc,pdf,excel,ppt'])->move(ROOT_PATH  . 'public/uploads/'.$model);
        if($info){
            // 成功上传后 获取上传信息
              $path="public/uploads/".$model."/".$info->getSaveName();
              $data["path"]='/'.str_replace("\\","/",$path);
			  $data["oldname"]=$_FILES[$name]["name"];
			  $data["exts"]=$info->getExtension();
		      $data["status"]=1;
			  $data["size"]=$_FILES[$name]["size"];
		      $data["create_time"]=time();
			  $id=db('attachment')->insertGetId($data);
				// 返回
			//header('content-type:application/json;charset=utf-8');
			$INSTALL_PATH = str_replace('\\','/',dirname($_SERVER['SCRIPT_NAME']));
			if($INSTALL_PATH==="/"){
				  $INSTALL_PATH="";		
			}else{
				$INSTALL_PATH= '/'. trim($INSTALL_PATH,'/').'';
			}
			$return = array('url'=>$INSTALL_PATH.$data["path"],'error'=>0,'msg'=>'上传成功','id'=>$id,"status"=>200);
			exit(json_encode($return));

        }else{
            // 上传失败获取错误信息
			$msg=$file->getError();
			$data = array('path'=>'','msg'=>$msg,"code"=>0);
            exit (json_encode($data));
        }

		/* 返回JSON数据 */
		//exit(json_encode($return));
	}
	

}
