file('file'); // 获取上传的文件 if ($file == null) { return json(array('code' => 1, 'msg' => '未上传文件')); } // 获取文件后缀 $temp = explode(".", $_FILES["file"]["name"]); $extension = end($temp); // 判断文件是否合法 $allowedExtensions = array("jpg", "jpeg", "png", "pdf","xls","xlsx","pptx","rar","zip","docx","doc","ppt"); if (!in_array(strtolower($extension), $allowedExtensions)) { return json(array('code' => 1, 'msg' => '上传文件格式错误')); } // 移动文件到指定目录 $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'); if ($info) { $filePath = '/uploads/' . $info->getSaveName(); // $filePath = Config::get('file_server_url') . '/uploads/' . $info->getSaveName(); // $fileName = $info->getFilename(); // $fileType = $info->getMime(); // $fileSize = $info->getSize(); // $fileData = file_get_contents($info->getPathname()); if ($filePath) { return json(array('code' => 0, 'msg' => '文件上传成功', 'url' => $filePath)); } } else { // 上传失败获取错误信息 return json(array('code' => 1, 'msg' => $file->getError())); } } }