thinkphp 5 上传图片文件路径存并写入数据库!

public function Article_Add()
	{
		$title = input('param.title');
		$content = input('param.content');
		$postuser = input('param.postuser');
		$image = "";
		if($title <> '' and $content <> '')
		{
//			$data = [
//						['title' => $title, 'content' => $content ,'postuser' => $postuser ,'posttime' => time()]
//					];
//			$db = db('Article') -> insertAll($data);
//			
			// 获取表单上传文件 例如上传了001.jpg
		    $file = request()->file('image');
		    // 移动到框架应用根目录/public/uploads/ 目录下
		    if($file){
		        $info = $file->move(ROOT_PATH . 'static' . DS . 'uploads');
		        if($info){
		            // 成功上传后 获取上传信息
		            // 输出 jpg
		            echo $info->getExtension();
		            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
		            echo $info->getSaveName();
		            // 输出 42a79759f284b767dfcb2a0197904287.jpg
		            echo $info->getFilename(); 
					$image = $info->getSaveName();
		        }else{
		            // 上传失败获取错误信息
		            echo $file->getError();
		        }
		    }
			$data = [
						['title' => $title, 'content' => $content ,'postuser' => $postuser ,'image' => $image , 'posttime' => time()]
					];
			$db = db('Article') -> insertAll($data);
			
			return $this -> success('发布成功','Article_Add');			
		}

		//$data = [
	    //['title' => '吴胜勇', 'content' => '今天学习了tp5开发啊啊啊','posttime' => time() ,'postuser' => '吴胜勇'],
	    //['title' => '吴胜勇', 'content' => '今天学习了tp5开发啊啊啊', 'posttime' => time() ,'postuser' => '吴胜勇'],
	    //['title' => '吴胜勇', 'content' => '今天学习了tp5开发啊啊啊', 'posttime' => time() ,'postuser' => '吴胜勇'],
		//];
		//$db = db('Article') -> insertAll($data);
	 	//dump($data);	
		return $this->fetch();
	}

你可能感兴趣的:(ThinkPHP,数据库,thinkphp5,php)