123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace Admin\Model;
- use Think\Model;
- /**
- * 用户模型
- * @author Devil
- * @blog http://gong.gg/
- * @version 0.0.1
- * @datetime 2016-12-01T21:51:08+0800
- */
- class UploaderModel extends CommonModel {
- public function Uploader($path) {
- $files = array();
- $success = 0; //用户统计有多少张图片上传成功了
- // var_dump($_FILES);die;
- foreach ($_FILES as $item) {
- $index = count($files);
-
- $files[$index]['srcName'] = $item['name']; //上传图片的原名字
- $files[$index]['error'] = $item['error']; //和该文件上传相关的错误代码
- $files[$index]['size'] = $item['size']; //已上传文件的大小,单位为字节
- $files[$index]['type'] = $item['type']; //文件的 MIME 类型,需要浏览器提供该信息的支持,例如"image/gif"
- $files[$index]['success'] = false; //这个用于标志该图片是否上传成功
- $files[$index]['path'] = ''; //存图片路径
- // 接收过程有没有错误
- if ($item['error'] != 0)
- continue;
- //判断图片能不能上传
- if (!is_uploaded_file($item['tmp_name'])) {
- $files[$index]['error'] = 8000;
- continue;
- }
- //扩展名
- $extension = '';
- if (strcmp($item['type'], 'image/jpeg') == 0) {
- $extension = '.jpg';
- } else if (strcmp($item['type'], 'image/png') == 0) {
- $extension = '.png';
- } else if (strcmp($item['type'], 'image/gif') == 0) {
- $extension = '.gif';
- } else {
- //如果type不是以上三者,我们就从图片原名称里面去截取判断去取得(处于严谨性)
- $substr = strrchr($item['name'], '.');
- if (FALSE == $substr) {
- $files[$index]['error'] = 8002;
- continue;
- }
- //取得元名字的扩展名后,再通过扩展名去给type赋上对应的值
- if (strcasecmp($substr, '.jpg') == 0 || strcasecmp($substr, '.jpeg') == 0 || strcasecmp($substr, '.jfif') == 0 || strcasecmp($substr, '.jpe') == 0) {
- $files[$index]['type'] = 'image/jpeg';
- } else if (strcasecmp($substr, '.png') == 0) {
- $files[$index]['type'] = 'image/png';
- } else if (strcasecmp($substr, '.gif') == 0) {
- $files[$index]['type'] = 'image/gif';
- }else if (strcasecmp($substr, '.webp') == 0){
- $files[$index]['type'] = 'image/webp';
- }else if (strcasecmp($substr, '.xls') == 0){
- $files[$index]['type'] = 'image/xls';
- } else {
- $files[$index]['error'] = 8003;
- continue;
- }
- $extension = $substr;
- }
- //对临时文件名加密,用于后面生成复杂的新文件名
- $md5 = md5_file($item['tmp_name']);
- //取得图片的大小
- $imageInfo = getimagesize($item['tmp_name']);
- $rawImageWidth = $imageInfo[0];
- $rawImageHeight = $imageInfo[1];
- //设置图片上传路径,放在upload文件夹,以年月日生成文件夹分类存储,
- //rtrim(base_url(), '/')其实就是网站的根目录,大家自己处理
- //define('BASE_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/");
- //$path = BASE_PATH . 'upload/' . date('Ymd') . '/';
- //确保目录可写
- $this->ensure_writable_dir($path);
- //文件名
- $name = "$md5.0x{$rawImageWidth}x{$rawImageHeight}{$extension}";
- //加入图片文件没变化到,也就是存在,就不必重复上传了,不存在则上传
- $ret = file_exists($path . $name) ? true : move_uploaded_file($item['tmp_name'], $path . $name);
- if ($ret === false) {
- $files[$index]['error'] = 8004;
- continue;
- } else {
- $files[$index]['path'] = $path . $name; //存图片路径
- $files[$index]['success'] = true; //图片上传成功标志
- $files[$index]['width'] = $rawImageWidth; //图片宽度
- $files[$index]['height'] = $rawImageHeight; //图片高度
- $success ++; //成功+1
- }
- }
- $num = 0;
- if($files[$index]['type'] == 'image/xls'){
- $file = $files[$index]['path'];
- $title = array(
- 'title' => array('col' => 'A', 'name' => '资料标题'),
- );
- $excel = new \My\Excel(array('title'=>$title,'msg'=>L('common_not_data_tips')));
- $data = $excel->Import($file);
- $num = $this->add_robots_info($data);
- }
- //将图片已json形式返回给js处理页面 ,这里大家可以改成自己的json返回处理代码
- echo json_encode(array(
- 'total' => count($files),
- 'success' => $success,
- 'imgurl' => $path . $name,
- 'num' => $num,
- ));
- //echo $path . $name;
- }
- function ensure_writable_dir($dir) {
- if (!file_exists($dir)) {
- mkdir($dir, 0766, true);
- chmod($dir, 0766);
- chmod($dir, 0777);
- } else if (!is_writable($dir)) {
- chmod($dir, 0766);
- chmod($dir, 0777);
- if (!is_writable($dir)) {
- throw new FileSystemException("目录 $dir 不可写");
- }
- }
- }
-
- public function add_robots_info($data){
- $i = 0;
- foreach($data as $v){
- $info['creat_at'] = date('Y-m-d H:i:s', time());
- $info['title'] = $v['title'];
- M('robots')->add($info);
- $i++;
- }
- return $i;
- }
- }
- ?>
|