<?php
namespace Smi\Controller;
use Think\Controller;
header("Content-type:text/html;charset=utf-8");
class ToolsController extends Controller 
{
	public function __construct()
	{
		parent::__construct();
	}

	/**
     * 单文件上传
    */
  	public static function upload($save_path, $name = '')
  	{	
  		$upload = new \Think\Upload();// 实例化上传类
    	$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath = $save_path; // 设置附件上传目录
		// 上传文件
		if (!empty($name)) {
			$info = $upload->uploadOne($_FILES[$name]);
		} else {
			$info = $upload->upload();
		}
		if(!$info) {// 上传错误提示错误信息
			return array('status' => -1, 'msg' => $upload->getError());
		}else{// 上传成功
			$return = '';
			if (!empty($name)) {
				$return = $info['savepath'].$info['savename'];
			} else {
				foreach ($info as $v) {
					$return = $v['savepath'].$v['savename'];
				}	
			}

			return array('status' => 1, 'file_path' => $return);
		}
	}

	/**
     * 多文件上传
    */
    public static function uploads($save_path)
    {	
    	$upload = new \Think\Upload();// 实例化上传类
    	$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath = $save_path; // 设置附件上传目录
		// 上传文件
		$info = $upload->upload();
		
    	if(!$info) {// 上传错误提示错误信息
			return array('status' => -1, 'msg' => $upload->getError());
		}else{// 上传成功
			$return = array();
			foreach ($info as $k => $v) {
				$return[] = $v['savepath'].$v['savename'];
			}

			return array('status' => 1, 'file_path' => $return);
		}
    }

    /**
     * 文件上传处理
     */
    private static function do_upload($save_path, $upload)
    {
		$upload->maxSize = 3145728 ;// 设置附件上传大小
		$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath = $save_path; // 设置附件上传目录
		// 上传文件
		$info = $upload->upload();

		return $info;
    }
}