TP5.0 图片上传

if (\request()->isPost()) {
$data = input('post.');
$file = \request()->file('pic');//获取图片
if($file){
if (true !== $this->validate(['pic' => $file], ['pic' => 'require|image'])) {
$this->error('请选择图像文件/或者图片格式不正确');
}else{
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
$data['pic'] = DS . 'uploads' . DS . $info->getSaveName();//存入字段的路径字符串
}else{
$this->error('请选择图像文件/或者图片格式不正确');
}
}
//修改前删除图片
$pic = $compay->where('id',1)->value('pic');
     if($pic)
       $path = 'public/'.$pic;//定义文件存放的路径
       $this->unlink($path)
     }

}else{
$data['pic'] = $compay->where('id',1)->value('pic');
$path = '';//定义文件存放的路径
}
  
    if($compay->where('id',1)->update($data)){
$this->success('上传完成');
}


}


public function unlink($path)//删除原有文件(本地文件释放资源)
{
return is_file($path) && unlink($path);
}
posted @ 2019-01-22 18:48  田园猫迪  阅读(640)  评论(0)    收藏  举报