当前位置:   article > 正文

thinkphp6入门(20)-- 如何上传图片、文件_tp6 文件上传

tp6 文件上传

1. 配置文件

设置上传的路径

图片

对应文件夹

图片

2. 前端

  1. <div class="card-body">
  2. <h1 class="card-title">用户头像</h1>
  3. <img src="../../../uploads/{$user.avatar_photo_path}" alt="avatar" height="100"/>
  4. <form class="forms-sample" action="保存路径" method="post" enctype="multipart/form-data">
  5. 选择图片(最大0.5M,格式jpg,jpeg,png):<input type="file" name="user_avatar_image"/>
  6. <button type="submit" class="btn btn-primary mr-2">上传</button>
  7. </form>
  8. </div>

3. 后端

  1. /**
  2. * 保存用户头像
  3. */
  4. function do_save_user_avatar()
  5. {
  6. // 字段名
  7. $field_name = 'user_avatar_image';
  8. if(request()->isPost()){
  9. try {
  10. // 获取表单上传文件 例如上传了001.jpg
  11. $file = request()->file($field_name);
  12. // 使用验证器验证上传的文件
  13. validate(['file' => [
  14. // 限制文件大小(单位b),这里限制为1M
  15. 'fileSize' => 0.5 * 1024 * 1024,
  16. // 限制文件后缀,多个后缀以英文逗号分割
  17. 'fileExt' => 'jpg,jpeg,png'
  18. ]])->check(['file' => $file]);
  19. // 上传到本地服务器
  20. // public/uploads文件夹下的profile文件夹
  21. $save_name = \think\facade\Filesystem::disk('public')->putFile('profile', $file);
  22. if($save_name){
  23. // 要更新的数据库
  24. //
  25. // 上传之后的操作
  26. return $this->success('上传成功', '跳转路径');
  27. }
  28. }
  29. catch (\Throwable $e) {
  30. return $this->error('上传图片失败,' . $e->getMessage());
  31. }
  32. }
  33. }

注意:try catch中捕捉错误,是\Throwable $e

也可单独封装出一个上传文件的函数,包含压缩图片

  1. // 需要安装composer require topthink/think-image
  2. /**
  3. * 上传文件
  4. * $field_name = 'user_avatar_image';
  5. * $subdirectory public下的哪个子目录
  6. * $fileSize = 0.5 (单位为M)
  7. * $compressionRatio = 0.8 压缩成原来的80%
  8. * $fileExt 文件格式字符串
  9. * $is_image_thumb = 0, 图片是否压缩
  10. * $thumb_threshold = 0.5, 图片的size大于多少才压缩,单位M
  11. * $thumb_width = 500, 压缩的宽
  12. * $thumb_height = 500, 压缩的高
  13. */
  14. function uploadFile($field_name, $subDirectory, $fileSize = 1, $fileExt = 'jpg,jpeg,png', $is_image_thumb = 0, $thumb_threshold = 0.5, $thumb_width = 500, $thumb_height = 500)
  15. {
  16. // 获取表单上传文件 例如字段名是'user_avatar_image'
  17. if(!array_key_exists($field_name, $_FILES))
  18. {
  19. $response = [];
  20. $response['code'] = 100;
  21. $response['msg'] = 'FILES中不存在该变量';
  22. $response['data'] = [];
  23. return $response;
  24. }
  25. else
  26. {
  27. // 变量'user_avatar_image'没有值
  28. if($_FILES[$field_name]['error'] != 0){
  29. $response = [];
  30. $response['code'] = 200;
  31. $response['msg'] = '没有上传文件';
  32. $response['data'] = [];
  33. return $response;
  34. }
  35. }
  36. if(request()->isPost()){
  37. try {
  38. // 获取表单上传文件 例如上传了001.jpg
  39. $file = request()->file($field_name);
  40. // 使用验证器验证上传的文件
  41. validate(['file' => [
  42. // 限制文件大小(单位b),这里限制为1M
  43. 'fileSize' => $fileSize * 1024 * 1024,
  44. // 限制文件后缀,多个后缀以英文逗号分割
  45. 'fileExt' => $fileExt
  46. ]])->check(['file' => $file]);
  47. // 上传到本地服务器
  48. // uploads文件夹下的profile文件夹
  49. $save_name = '';
  50. // 是否需要压缩
  51. if($is_image_thumb == 0 || $file->getSize() < $thumb_threshold * 1024 * 1024)
  52. {
  53. // 路径有日期文件夹
  54. $save_name = \think\facade\Filesystem::disk('public')->putFile($subDirectory, $file);
  55. }
  56. else
  57. {
  58. // 获取上传的图片,进行图片压缩
  59. $image = \think\Image::open($file);
  60. // 保存图片的路径处理
  61. $date = date('Ymd');
  62. $save_name = public_path() . 'uploads/' . $subDirectory . '/' . $date . '_' . $file->md5() . '.jpg';
  63. // 保存
  64. // 默认会按比例保存,但是最大宽度、高度不超过thumb(400, 400)设定值
  65. $img_edit = $image->thumb($thumb_width, $thumb_height)->save($save_name);
  66. }
  67. if($save_name){
  68. $response = [];
  69. $response['code'] = 1;
  70. $response['msg'] = '上传成功';
  71. $response['data']['save_name'] = $save_name;
  72. return $response;
  73. }
  74. }
  75. catch (\Throwable $e) {
  76. $response = [];
  77. $response['code'] = 300;
  78. $response['msg'] = '上传失败,' . $e->getMessage();
  79. $response['data'] = [];
  80. return $response;
  81. }
  82. }
  83. $response = [];
  84. $response['code'] = 400;
  85. $response['msg'] = '上传失败,非POST请求';
  86. $response['data'] = [];
  87. return $response;
  88. }

上传成功之后,如图

图片

by 软件工程小施同学

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/970956
推荐阅读
相关标签
  

闽ICP备14008679号