TP5的图片上传

public function upload(){
// 获取表单上传文件 例如上传了001.jpg
$file = request()->file('image');
$picture=model("Picture");
$turepath = $file->getRealPath();
$md5path=md5_file($turepath);
$shalpath=sha1_file($turepath);
$map["md5"]=$md5path;
$map["sha1"]=$shalpath;
/*判断是否有相同图片上传过了*/
$image=$picture->find($map);
if($image){
$this->output_data(200,'success',array('img'=>'http://'.$_SERVER['HTTP_HOST'].$image["path"],"id"=>$image["id"]));
}
//判断文件类型
if(!count($file)){
$this->output_data(-1,'图片不能为空!');
}
$type=$file->checkImg();
if(!$type){
$this->output_data(-1,'图片类型不正确!');
}

// 移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
$path = $info->getSaveName();
$data=array();
$img_path='http://'.$_SERVER['HTTP_HOST'].'/uploads/'.$path;
$type=$info->getExtension();
$type="image/".$type;
$data["path"]='/uploads/'.$path;
$data["type"]=$type;
$data["status"]=1;
$data["md5"]=$md5path;
$data["sha1"]=$shalpath;

$result=$picture->insert($data);
$this->output_data(200,'success',array('img'=>$img_path,"id"=>$result));
/*echo $path;
echo "<br>";
// 输出 42a79759f284b767dfcb2a0197904287.jpg
echo $info->getFilename();
echo "<br>";
输出 42a79759f284b767dfcb2a0197904287.jpg*/
echo $info->getPathName();
}else{
// 上传失败获取错误信息
$this->output_data(-1,'提交失败!');
}
}