当前位置:Gxlcms > php框架 > PHP Laravel 上传图片、文件等类封装

PHP Laravel 上传图片、文件等类封装

时间:2021-07-01 10:21:17 帮助过:12人阅读

今天把项目中上传功能封装成类,方便后面使用,简单的封装了一下,感觉还不怎么好,后面继续优化。

具体代码如下:

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wady www.bcty365.com
  5. * Date: 2017/8/16
  6. * Time: 14:52
  7. */
  8. namespace App\ThinkClass;
  9. use Symfony\Component\HttpFoundation\File\UploadedFile;
  10. class UploadClass
  11. {
  12. /**
  13. * @var UploadedFile $file;
  14. */
  15. protected $file;
  16. /**
  17. * 上传错误信息
  18. * @var string
  19. */
  20. private $error = ''; //上传错误信息
  21. private $fullPath='';//绝对地址
  22. private $config = array(
  23. 'maxSize' => 3*1024*1024, //上传的文件大小限制 (0-不做限制)
  24. 'exts' => array('jpg','jpeg','gif','png','doc','docx','xls','xlsx','ppt','pptx','pdf','rar','zip'), //允许上传的文件后缀
  25. 'subName' => '', //子目录创建方式,[0]-函数名,[1]-参数,多个参数使用数组
  26. 'rootPath' => '/uploads/', //保存根路径
  27. 'savePath' => '', //保存路径
  28. 'thumb' => array(),//是裁剪压缩比例
  29. );
  30. public function __construct($config = array()){
  31. /* 获取配置 */
  32. $this->config = array_merge($this->config, $config);
  33. if(!emptyempty($this->config['exts'])){
  34. if (is_string($this->exts)){
  35. $this->config['exts'] = explode(',', $this->exts);
  36. }
  37. $this->config['exts'] = array_map('strtolower', $this->exts);
  38. }
  39. $this->config['subName'] = $this->subName ? ltrim($this->subName,'/') : '/'.date('Ymd');
  40. $this->fullPath = rtrim(public_path(),'/').$this->config['rootPath'];
  41. }
  42. public function __get($name) {
  43. return $this->config[$name];
  44. }
  45. public function __set($name,$value){
  46. if(isset($this->config[$name])) {
  47. $this->config[$name] = $value;
  48. }
  49. }
  50. public function __isset($name){
  51. return isset($this->config[$name]);
  52. }
  53. /**
  54. * 获取最后一次上传错误信息
  55. * @return string 错误信息
  56. */
  57. public function getError(){
  58. return $this->error;
  59. }
  60. public function upload($file){
  61. if(emptyempty($file)){
  62. $this->error = '没有上传的文件';
  63. return false;
  64. }
  65. if(!$this->checkRootPath($this->fullPath)){
  66. $this->error = $this->getError();
  67. return false;
  68. }
  69. $fileSavePath=$this->fullPath.$this->savePath.$this->subName;
  70. if(!$this->checkSavePath($fileSavePath)){
  71. $this->error = $this->getError();
  72. return false;
  73. }
  74. $files =array();
  75. if(!is_array($file)){
  76. //如果不是数组转成数组
  77. $files[]=$file;
  78. }else{
  79. $files=$file;
  80. }
  81. $info = array();
  82. $imgThumb = new \App\ThinkClass\ThumbClass();
  83. foreach ($files as $key=>$f){
  84. $this->file=$f;
  85. $f->ext = strtolower($f->getClientOriginalExtension());
  86. /*文件上传检查*/
  87. if (!$this->check($f)){
  88. continue;
  89. }
  90. $fileName = str_random(12).'.'.$f->ext;
  91. /* 保存文件 并记录保存成功的文件 */
  92. if ($this->file->move($fileSavePath,$fileName)) {
  93. /*图片按照宽高比例压缩*/
  94. \Log::notice($fileSavePath.$fileName);
  95. if(!emptyempty($this->thumb) && is_array($this->thumb)){
  96. $imgThumb ->thumb($this->thumb,$fileSavePath.'/'.$fileName);
  97. }
  98. $info[]=$this->rootPath.$this->savePath.$this->subName.'/'.$fileName;
  99. }
  100. }
  101. return is_array($info) ? $info : false;
  102. }
  103. /**
  104. * 检测上传根目录
  105. * @param string $rootpath 根目录
  106. * @return boolean true-检测通过,false-检测失败
  107. */
  108. protected function checkRootPath($rootpath){
  109. if(!(is_dir($rootpath) && is_writable($rootpath))){
  110. $this->error = '上传根目录不存在!';
  111. return false;
  112. }
  113. return true;
  114. }
  115. /**
  116. * 检测上传目录
  117. * @param string $savepath 上传目录
  118. * @return boolean 检测结果,true-通过,false-失败
  119. */
  120. public function checkSavePath($savepath){
  121. /* 检测并创建目录 */
  122. if (!$this->mkdir($savepath )) {
  123. return false;
  124. } else {
  125. /* 检测目录是否可写 */
  126. if (!is_writable($savepath)) {
  127. $this->error = '上传目录不可写!';
  128. return false;
  129. } else {
  130. return true;
  131. }
  132. }
  133. }
  134. /**
  135. * 检查上传的文件
  136. * @param array $file 文件信息
  137. */
  138. private function check($file) {
  139. /* 检查文件大小 */
  140. if (!$this->checkSize($file->getSize())) {
  141. $this->error = '上传文件大小不符!';
  142. return false;
  143. }
  144. /* 检查文件后缀 */
  145. if (!$this->checkExt($file->ext)) {
  146. $this->error = '上传文件后缀不允许';
  147. return false;
  148. }
  149. /* 通过检测 */
  150. return true;
  151. }
  152. /**
  153. * 检查文件大小是否合法
  154. * @param integer $size 数据
  155. */
  156. private function checkSize($size) {
  157. return !($size > $this->maxSize) || (0 == $this->maxSize);
  158. }
  159. /**
  160. * 检查上传的文件后缀是否合法
  161. * @param string $ext 后缀
  162. */
  163. private function checkExt($ext) {
  164. return emptyempty($this->config['exts']) ? true : in_array(strtolower($ext), $this->exts);
  165. }
  166. /**
  167. * 创建目录
  168. * @param string $savepath 要创建的穆里
  169. * @return boolean 创建状态,true-成功,false-失败
  170. */
  171. protected function mkdir($savepath){
  172. if(is_dir($savepath)){
  173. return true;
  174. }
  175. if(mkdir($savepath, 0777, true)){
  176. return true;
  177. } else {
  178. $this->error = "目录创建失败";
  179. return false;
  180. }
  181. }
  182. }

使用案例:

头部引用  use App\ThinkClass\UploadClass; 

  1. $upload = new UploadClass();
  2. $upload->exts=array('jpg','png');
  3. $upload->maxSize=5*1024*1024;
  4. $upload->savePath='course/uid_6';
  5. $file = $request->file('fileImg');
  6. $aa = $upload->upload($file);
  7. dd($aa);

总结

以上所述是小编给大家介绍的PHP Laravel 上传图片、文件等类封装,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

人气教程排行