/** * 上传图片 * * @return array|\think\response\Json */ public function uploadImage(){ //获取上传的图片 $image = $_FILES; //判断获得变量 if ($image['file']['error'] > 0) { return json()->data(['code' => -1, 'msg' => '上传失败了']); } else { //截取文件后缀名 $type = strrchr($image['file']['name'], "."); //echo $type;die; //设置上传路径,我把它放在了upload下的interview目录下(需要在linux中给interview设置文件夹权限) $new_file = "./uploads/home/" . date('Y-m-d') . "/"; if (! file_exists ( $new_file )) { // 检查是否有该文件夹,如果没有就创建,并给予最高权限 mkdir ( $new_file, 0700 ); } $str_bh=substr(time(),-4); $str_token=substr(md5(time()),-4); $path = $new_file.$str_bh.$str_token.$image['file']['name']; //返回前台 $new_files = "uploads/home/" . date('Y-m-d') . "/"; $paths = $new_files.$str_bh.$str_token.$image['file']['name']; //判断上传的文件是否为图片格式 //将图片文件移到该目录下 move_uploaded_file($image['file']['tmp_name'], $path); $url = 'https://'.$_SERVER['HTTP_HOST']; $res_data = array( 'file'=>$paths, ); return json()->data(['code' => 1, 'msg' => '上传成功','data'=>$res_data]); } }