UploaderModel.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Admin\Model;
  3. use Think\Model;
  4. /**
  5. * 用户模型
  6. * @author Devil
  7. * @blog http://gong.gg/
  8. * @version 0.0.1
  9. * @datetime 2016-12-01T21:51:08+0800
  10. */
  11. class UploaderModel extends CommonModel {
  12. public function Uploader($path) {
  13. $files = array();
  14. $success = 0; //用户统计有多少张图片上传成功了
  15. // var_dump($_FILES);die;
  16. foreach ($_FILES as $item) {
  17. $index = count($files);
  18. $files[$index]['srcName'] = $item['name']; //上传图片的原名字
  19. $files[$index]['error'] = $item['error']; //和该文件上传相关的错误代码
  20. $files[$index]['size'] = $item['size']; //已上传文件的大小,单位为字节
  21. $files[$index]['type'] = $item['type']; //文件的 MIME 类型,需要浏览器提供该信息的支持,例如"image/gif"
  22. $files[$index]['success'] = false; //这个用于标志该图片是否上传成功
  23. $files[$index]['path'] = ''; //存图片路径
  24. // 接收过程有没有错误
  25. if ($item['error'] != 0)
  26. continue;
  27. //判断图片能不能上传
  28. if (!is_uploaded_file($item['tmp_name'])) {
  29. $files[$index]['error'] = 8000;
  30. continue;
  31. }
  32. //扩展名
  33. $extension = '';
  34. if (strcmp($item['type'], 'image/jpeg') == 0) {
  35. $extension = '.jpg';
  36. } else if (strcmp($item['type'], 'image/png') == 0) {
  37. $extension = '.png';
  38. } else if (strcmp($item['type'], 'image/gif') == 0) {
  39. $extension = '.gif';
  40. } else {
  41. //如果type不是以上三者,我们就从图片原名称里面去截取判断去取得(处于严谨性)
  42. $substr = strrchr($item['name'], '.');
  43. if (FALSE == $substr) {
  44. $files[$index]['error'] = 8002;
  45. continue;
  46. }
  47. //取得元名字的扩展名后,再通过扩展名去给type赋上对应的值
  48. if (strcasecmp($substr, '.jpg') == 0 || strcasecmp($substr, '.jpeg') == 0 || strcasecmp($substr, '.jfif') == 0 || strcasecmp($substr, '.jpe') == 0) {
  49. $files[$index]['type'] = 'image/jpeg';
  50. } else if (strcasecmp($substr, '.png') == 0) {
  51. $files[$index]['type'] = 'image/png';
  52. } else if (strcasecmp($substr, '.gif') == 0) {
  53. $files[$index]['type'] = 'image/gif';
  54. }else if (strcasecmp($substr, '.webp') == 0){
  55. $files[$index]['type'] = 'image/webp';
  56. }else if (strcasecmp($substr, '.xls') == 0){
  57. $files[$index]['type'] = 'image/xls';
  58. } else {
  59. $files[$index]['error'] = 8003;
  60. continue;
  61. }
  62. $extension = $substr;
  63. }
  64. //对临时文件名加密,用于后面生成复杂的新文件名
  65. $md5 = md5_file($item['tmp_name']);
  66. //取得图片的大小
  67. $imageInfo = getimagesize($item['tmp_name']);
  68. $rawImageWidth = $imageInfo[0];
  69. $rawImageHeight = $imageInfo[1];
  70. //设置图片上传路径,放在upload文件夹,以年月日生成文件夹分类存储,
  71. //rtrim(base_url(), '/')其实就是网站的根目录,大家自己处理
  72. //define('BASE_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/");
  73. //$path = BASE_PATH . 'upload/' . date('Ymd') . '/';
  74. //确保目录可写
  75. $this->ensure_writable_dir($path);
  76. //文件名
  77. $name = "$md5.0x{$rawImageWidth}x{$rawImageHeight}{$extension}";
  78. //加入图片文件没变化到,也就是存在,就不必重复上传了,不存在则上传
  79. $ret = file_exists($path . $name) ? true : move_uploaded_file($item['tmp_name'], $path . $name);
  80. if ($ret === false) {
  81. $files[$index]['error'] = 8004;
  82. continue;
  83. } else {
  84. $files[$index]['path'] = $path . $name; //存图片路径
  85. $files[$index]['success'] = true; //图片上传成功标志
  86. $files[$index]['width'] = $rawImageWidth; //图片宽度
  87. $files[$index]['height'] = $rawImageHeight; //图片高度
  88. $success ++; //成功+1
  89. }
  90. }
  91. $num = 0;
  92. if($files[$index]['type'] == 'image/xls'){
  93. $file = $files[$index]['path'];
  94. $title = array(
  95. 'title' => array('col' => 'A', 'name' => '资料标题'),
  96. );
  97. $excel = new \My\Excel(array('title'=>$title,'msg'=>L('common_not_data_tips')));
  98. $data = $excel->Import($file);
  99. $num = $this->add_robots_info($data);
  100. }
  101. //将图片已json形式返回给js处理页面 ,这里大家可以改成自己的json返回处理代码
  102. echo json_encode(array(
  103. 'total' => count($files),
  104. 'success' => $success,
  105. 'imgurl' => $path . $name,
  106. 'num' => $num,
  107. ));
  108. //echo $path . $name;
  109. }
  110. function ensure_writable_dir($dir) {
  111. if (!file_exists($dir)) {
  112. mkdir($dir, 0766, true);
  113. chmod($dir, 0766);
  114. chmod($dir, 0777);
  115. } else if (!is_writable($dir)) {
  116. chmod($dir, 0766);
  117. chmod($dir, 0777);
  118. if (!is_writable($dir)) {
  119. throw new FileSystemException("目录 $dir 不可写");
  120. }
  121. }
  122. }
  123. public function add_robots_info($data){
  124. $i = 0;
  125. foreach($data as $v){
  126. $info['creat_at'] = date('Y-m-d H:i:s', time());
  127. $info['title'] = $v['title'];
  128. M('robots')->add($info);
  129. $i++;
  130. }
  131. return $i;
  132. }
  133. }
  134. ?>