TP5.1上传文件



namespace app\common;

use think\Controller;
use Request,Log,Config;

class Upload extends Controller
{
    protected $size;
    protected $savePath;
    protected $i;
    /**
     * 文件上传处理
     * @return \think\response\Json
     */
    public function upload($file, $num=1){
        //获取upload配置信息
        $path   = config('common.app_path');
        $config = config('upload.');
        $this->size   = $config['img_size'];
        $this->savePath = $config['save_path'] . DIRECTORY_SEPARATOR . 'images';
        $this->i = $config['compression_size'];
        if(1 == $num){
            $info = $file->validate(['size'=>$this->size])
                ->move($this->savePath);
            if($info){
                $url1 = dirname($_SERVER['SCRIPT_FILENAME']) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $info->getSaveName();
                //生成缩略图
                $image = Image::open($url1);

                $width = $image->width() * $this->i;
                $height = $image->height() * $this->i;
                $image->thumb($width, $height, Image::THUMB_CENTER)->save($url1,'jpg',30,true);
                $path = $path. DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $info->getSaveName();
                $path = str_replace('\\', '/', $path);
                return $path;
            }else{
                return $file->getError();
            }
        }else{
            return $this->manyUpload($file);
        }
    }
    //多图
    public function manyUpload($files){
        $url   = config('common.app_path');
        $path  = null;
        foreach($files as $file) {                      //多图上传关键在这
            //验证规则等,在配置文件中读取
            $info = $file->validate(['size'=>$this->size])
                ->move($this->savePath);
            if ($info) {
                $url1 = dirname($_SERVER['SCRIPT_FILENAME']) . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $info->getSaveName();
                //生成缩略图
                $image = Image::open($url1);
                $width = $image->width() * $this->i;
                $height = $image->height() * $this->i;
                $image->thumb($width, $height, Image::THUMB_SCALING)->save($url1,'jpg',30,true);
                
                $path .= $url . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . $info->getSaveName() . "|";
            } else {
                // 上传失败获取错误信息
                return $file->getError();
            }
        }
        $path = str_replace('\\', '/', $path);
        return rtrim($path, '|');

    }
    /*base64图片*/
    function Base64Upload($base64) {
        $url   = config('common.app_path');
        $path  = null;
        $result = array();
        foreach ($base64 as $k => $v) {
            $path = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;


            //保存base64字符串为图片
            //匹配出图片的格式
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $v, $re)){
                $type = $re[2];

                $new_file = $path . date('Ymd',time())."/";

                if(!file_exists($new_file))
                {
                    //检查是否有该文件夹,如果没有就创建,并给予最高权限
                    mkdir(iconv("UTF-8", "GBK", $new_file),0777,true);
                    @chmod($new_file, 0777);
                }
                //文件名
                $file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $type;
                //文件存放路径
                $new_file = $new_file . $file_name;

              if (file_put_contents($new_file, base64_decode(str_replace($re[1], '', $v)))){
                    $result[$k] = $url . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . date('Ymd',time()) . DIRECTORY_SEPARATOR . $file_name;
                    $result[$k] = str_replace('\\', '/', $result[$k]);
                // echo '新文件保存成功:', $new_file;
              }
              
            }
        }
        return $result;
    }
    /**
    * 上传文件
    */
    public function uploadFile($file)
    {
        //获取upload配置信息
        $path   = config('common.app_path');
        $config = config('upload.');
        $size   = $config['file_size'];
        $savePath = $config['save_path'] . DIRECTORY_SEPARATOR . 'files';
        $info = $file->validate(['size'=>$size])
            ->move($savePath);


        if($info){
            $path = $path. DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . $info->getSaveName();
            $path = str_replace('\\', '/', $path);
            return $path;
        }else{
            return $file->getError();
        }
    }


}

你可能感兴趣的:(php,TP5.1)