php上传文件到服务器

type是根据不同的文件类型,在方法中可以进行不同的处理

  /**
     * [上传音频文件到服务器]
     * @param  [object] $file [文件资源]
     * @param  [string] $type [图片类型]
     * @return [string]       [图片在服务器的路径]
     */
    public function uploadFiles02($file, $type = '') {
        $info = $file->move(ROOT_PATH . 'public' . DS . 'temp01');
        if ($info) {
            //获取图片路径
            $path = '/temp01/' . $info->getSaveName();
            $path = preg_replace('/\\\\/', '/', $path);
            //裁剪图片
            if (!empty($type)) {
                $this->imageEdit($path, $type);
            }
        } else {
            $this->returnMsg(400, $file->getError());
        }

        return $path;
    }

调用上述方法

 //1. 接收参数
$this->datas = $this->params;
 //2. 上传文件获取路径
 $head_img_path = $this->uploadFiles($this->datas['user_icon'], 'head_img');

除此之外,还有下面这个方式,当然很类似,只是根据不同的场景使用

 /**
     * [上传文件到服务器]
     * @param  [object] $file [文件资源]
     * @param  [string] $type [图片类型]
     * @return [string]       [图片在服务器的路径]
     */
    public function uploadFiles($file, $type = '')
    {
        $name = $file->getInfo()['name'];
        //dump($file);
        //dump($name);die;
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads',$name);
        if ($info) {
            //获取图片路径
            $path = '/uploads/' . $info->getSaveName();
            $path = iconv("GB2312","UTF-8",  $path);
            $path = preg_replace('/\\\\/', '/', $path);
            //裁剪图片
            if (!empty($type)) {
                $this->imageEdit($path, $type);
            }
        } else {
            $this->returnMsg(400, $file->getError());
        }

        return $path;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值