thinkphp3 在linux系统上传图片总是报错没有上传根目录,请手动创建

解决办法:

$upload->rootPath  = './Public/Uploads/'; // 设置附件上传根目录          
$upload->savePath =__ROOT__.'/Public/Uploads/';

例子:添加文章(带上传图片)

控制器

public function add(){
        $cate=D('category');
        $cateres=$cate->catetree();
        $this->assign('cateres',$cateres);

        $article=D('article');
        //添加操作
        if(IS_POST){
            $data['ar_title']=I('ar_title');
            $data['ar_author']=I('ar_author');
            $data['ar_rem']=I('ar_rem');
            $data['ar_cateid']=I('ar_cateid');
            $data['ar_content']=I('ar_content');
            $data['ar_time']=time();
            if($_FILES['ar_pic']['tmp_name']!=''){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                
        
                $upload->rootPath  = './Public/Uploads/'; // 设置附件上传根目录
             
                $upload->savePath =__ROOT__.'/Public/Uploads/';
                $info=$upload->uploadOne($_FILES['ar_pic']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                 }else{// 上传成功    
                      $data['ar_pic']=$info['savepath'].$info['savename']; 
                     }
            }
            
            if($article->create($data)){
                if($article->add()){
                    $this->success('文章新增成功',U('lst'));
                }else{
                    $this->error('文章新增失败!');
                }
            }else{
                $this->error($article->getError());
            }
        return;
        }
        $this->display();
    }

此时图片上传的目录为   www.xxx.com/Public/Uploads/Public/Uploads/

模板中调用

<td height="20" bgcolor="#FFFFFF" class="STYLE19"><div align="center">
          <if condition="$vo[ar_pic] neq ''">
          <img src="/Public/Uploads/{$vo.ar_pic}" height="50" />
          <else />
          暂无缩略图
          </if>
        </div></td>

猜你喜欢

转载自blog.csdn.net/qq_40270754/article/details/86562063