|
|
@@ -1769,70 +1769,76 @@ if (!function_exists('upload_image')) {
|
|
|
*/
|
|
|
function upload_image($request, $form_name = 'file', $file_path = '', $userId = 0)
|
|
|
{
|
|
|
- // 检测请求中是否包含name=$form_name的上传文件
|
|
|
- $params = $request->post();
|
|
|
- if (!$request->hasFile($form_name)) {
|
|
|
- return message("请上传文件", false);
|
|
|
- }
|
|
|
- // 文件对象
|
|
|
- $file = $request->file($form_name);
|
|
|
- // 判断图片上传是否错误
|
|
|
- if (!$file->isValid()) {
|
|
|
- // 文件上传失败
|
|
|
- return message("上传文件验证失败", false);
|
|
|
- }
|
|
|
- // 文件原名
|
|
|
- $original_name = $file->getClientOriginalName();
|
|
|
- // 文件扩展名(文件后缀)
|
|
|
- $ext = $file->getClientOriginalExtension();
|
|
|
- // 临时文件的绝对路径
|
|
|
- $real_path = $file->getRealPath();
|
|
|
- // 文件类型
|
|
|
- $type = $file->getClientMimeType();
|
|
|
- // 文件大小
|
|
|
- $size = $file->getSize();
|
|
|
+ try {
|
|
|
+ // 检测请求中是否包含name=$form_name的上传文件
|
|
|
+ $params = $request->post();
|
|
|
+ if (!$request->hasFile($form_name)) {
|
|
|
+ return message("请上传文件", false);
|
|
|
+ }
|
|
|
+ // 文件对象
|
|
|
+ $file = $request->file($form_name);
|
|
|
+ // 判断图片上传是否错误
|
|
|
+ if (!$file->isValid()) {
|
|
|
+ // 文件上传失败
|
|
|
+ return message("上传文件验证失败", false);
|
|
|
+ }
|
|
|
+ // 文件原名
|
|
|
+ $original_name = $file->getClientOriginalName();
|
|
|
+ // 文件扩展名(文件后缀)
|
|
|
+ $ext = $file->getClientOriginalExtension();
|
|
|
+ // 临时文件的绝对路径
|
|
|
+ $real_path = $file->getRealPath();
|
|
|
+ // 文件类型
|
|
|
+ $type = $file->getClientMimeType();
|
|
|
+ // 文件大小
|
|
|
+ $size = $file->getSize();
|
|
|
+
|
|
|
+ // 文件大小校验
|
|
|
+ if ($size > 10 * 1024 * 1024) {
|
|
|
+ return message("文件大小超过了10M", false);
|
|
|
+ }
|
|
|
|
|
|
- // 文件大小校验
|
|
|
- if ($size > 10 * 1024 * 1024) {
|
|
|
- return message("文件大小超过了10M", false);
|
|
|
- }
|
|
|
+ // 文件后缀校验
|
|
|
+ $ext = strtolower($ext);
|
|
|
+ $ext_arr = array('jpg', 'jpeg', 'png', 'gif', 'doc', 'docx');
|
|
|
+ if (!in_array($ext, $ext_arr)) {
|
|
|
+ return message("文件格式不正确:" . $ext, false);
|
|
|
+ }
|
|
|
|
|
|
- // 文件后缀校验
|
|
|
- $ext = strtolower($ext);
|
|
|
- $ext_arr = array('jpg', 'jpeg', 'png', 'gif', 'doc', 'docx');
|
|
|
- if (!in_array($ext, $ext_arr)) {
|
|
|
- return message("文件格式不正确:" . $ext, false);
|
|
|
- }
|
|
|
+ // 文件路径
|
|
|
+ $file_dir = ($file_path ? ATTACHMENT_PATH . '/images/' . $file_path : UPLOAD_TEMP_PATH) . "/" . date('Ymd');
|
|
|
|
|
|
- // 文件路径
|
|
|
- $file_dir = ($file_path ? ATTACHMENT_PATH . '/images/' . $file_path : UPLOAD_TEMP_PATH) . "/" . date('Ymd');
|
|
|
+ // 检测文件路径是否存在,不存在则创建
|
|
|
+ if (!file_exists($file_dir)) {
|
|
|
+ mkdir($file_dir, 0777, true);
|
|
|
+ }
|
|
|
|
|
|
- // 检测文件路径是否存在,不存在则创建
|
|
|
- if (!file_exists($file_dir)) {
|
|
|
- mkdir($file_dir, 0777, true);
|
|
|
- }
|
|
|
+ // 文件名称(处理中文文件名,避免base64编码产生/字符导致路径问题)
|
|
|
+ $encoded_name = ($userId ? $userId . $original_name : $original_name);
|
|
|
+ $file_name = ($userId ? $userId . '_' : '') . str_replace(['=', '/', '+'], ['', '_', '-'], base64_encode($encoded_name));
|
|
|
+ $file_name = $file_name . '.' . $ext;
|
|
|
|
|
|
- // 文件名称
|
|
|
- $file_name = ($userId ? $userId . '_' . str_replace('=', '', base64_encode($userId . $original_name)) : str_replace('=', '', base64_encode($original_name)));
|
|
|
- $file_name = $file_name . '.' . $ext;
|
|
|
+ // 重命名保存
|
|
|
+ $path = $file->move($file_dir, $file_name);
|
|
|
|
|
|
- // 重命名保存
|
|
|
- $path = $file->move($file_dir, $file_name);
|
|
|
+ // 文件临时路径
|
|
|
+ $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
|
|
|
|
|
|
- // 文件临时路径
|
|
|
- $file_path = str_replace(ATTACHMENT_PATH, '', $file_dir) . '/' . $file_name;
|
|
|
+ // 返回结果
|
|
|
+ $result = [
|
|
|
+ 'img_original_name' => $original_name,
|
|
|
+ 'img_ext' => $ext,
|
|
|
+ 'img_real_path' => $real_path,
|
|
|
+ 'img_type' => $type,
|
|
|
+ 'img_size' => $size,
|
|
|
+ 'img_name' => $file_name,
|
|
|
+ 'img_path' => $file_path,
|
|
|
+ ];
|
|
|
|
|
|
- // 返回结果
|
|
|
- $result = [
|
|
|
- 'img_original_name' => $original_name,
|
|
|
- 'img_ext' => $ext,
|
|
|
- 'img_real_path' => $real_path,
|
|
|
- 'img_type' => $type,
|
|
|
- 'img_size' => $size,
|
|
|
- 'img_name' => $file_name,
|
|
|
- 'img_path' => $file_path,
|
|
|
- ];
|
|
|
- return message(MESSAGE_OK, true, $result);
|
|
|
+ return message(MESSAGE_OK, true, $result);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ return message("上传文件失败:" . $e->getMessage(), false);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|