码迷,mamicode.com
首页 > Web开发 > 详细

kindeditor 本地图片文件上传

时间:2015-04-08 15:14:56      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

一、文件或图片上传


try {
 if (!is_dir($this->uploadPath)) {
     throw new InvalidArgumentException(‘files.ourats.com还没搭建?‘);
 }
 if (!is_writable($this->uploadPath)) {
     throw new InvalidArgumentException(‘上传文件夹不可写‘);
 }
 // 如果该公司目录不存在,则创建之
 $ns = new Zend_Session_Namespace(‘DB‘);
 $cid = $ns->cid;
 $cidDirPath = $this->uploadPath . $cid;
 if (!is_dir($cidDirPath)) {
     $old = umask(0);
     if (!mkdir($cidDirPath, 0777)) {
         throw new InvalidArgumentException("创建$cid文件夹失败");
     }
     umask($old);
 }
 // 处理上传文件
 if (!isset($_GET[‘dir‘]) || ($_GET[‘dir‘] != ‘image‘ && $_GET[‘dir‘] != ‘file‘)) {
     throw new InvalidArgumentException(‘无效的参数‘);
 }
 if (!isset($_FILES[‘imgFile‘])) {
     throw new InvalidArgumentException(‘无效的参数‘);
 }
 if ($_FILES[‘imgFile‘][‘error‘] != UPLOAD_ERR_OK) {
     throw new InvalidArgumentException(‘上传失败‘);
 }
 // 如果文件名中有/\,则不接受, .和..也不行
 $filename = trim($_FILES[‘imgFile‘][‘name‘]);
 $len = strlen($filename);
 if ($len == 0 || $len > 255) {
     throw new InvalidArgumentException(‘无效的参数‘);
 }
 if (strpos($filename, ‘/‘) !== false
 || strpos($filename, ‘\\‘) !== false
 || $filename == ‘.‘
 || $filename == ‘..‘
 ) {
     throw new InvalidArgumentException(‘无效的参数‘);
 }
 // 如果文件已存在,则不接受
 $dstPath = $cidDirPath . ‘/‘ . $filename;
 if (file_exists($dstPath)) {
     throw new InvalidArgumentException(‘文件已经存在‘);
 }
 // 由于图片需要显示出来,所以必须严格验证
 $ext = explode(‘.‘, $filename);
 $ext = strtolower(array_pop($ext));
 $mimeTypes = array(
     ‘jpg‘ => ‘image/jpeg‘,
     ‘png‘ => ‘image/png‘,
     ‘gif‘ => ‘image/gif‘
 );
 if (isset($mimeTypes[$ext])) {
     $finfo = new finfo(FILEINFO_MIME_TYPE);
     $type = $finfo->file($_FILES[‘imgFile‘][‘tmp_name‘]);
     if ($type != $mimeTypes[$ext]) {
         throw new InvalidArgumentException(‘上传文件类型错误‘);
     }
     $_GET[‘dir‘] = ‘image‘;
 } elseif ($_GET[‘dir‘] == ‘image‘) {
     // 如果上传的文件不是图片,却又指明了要上传图片,则报错
     throw new InvalidArgumentException(‘上传图图片类型不支持‘);
 }
 if ($_GET[‘dir‘] == ‘image‘) {
     if ($_FILES[‘imgFile‘][‘size‘] > $this->maxImgSize) {
         throw new InvalidArgumentException(‘上传图片尺寸超过最大限制‘);
     }
 } else {
     if ($_FILES[‘imgFile‘][‘size‘] > $this->maxFileSize) {
         throw new InvalidArgumentException(‘上传文件尺寸超过最大限制‘);
     }
 }
 // 完成上传
 if (!move_uploaded_file($_FILES[‘imgFile‘][‘tmp_name‘], $dstPath)) {
    throw new InvalidArgumentException(‘上传失败‘);
 }
 $filename = $_FILES[‘imgFile‘][‘name‘];
 $args = array($this->token, $cid, $filename);
 sort($args, SORT_STRING);
 $sign = sha1(implode(‘‘, $args));
 $url = $this->filesUrl . ‘?cid=‘ . $cid . ‘&file=‘ . urlencode($filename) . ‘&sign=‘ . $sign;
 $res = array(‘error‘ => 0, ‘url‘ => $url, ‘filename‘ => $filename);
 } catch (InvalidArgumentException $e) {
     $res = array(‘error‘ => 1, ‘message‘ => $e->getMessage());
 } catch (Exception $e) {
     $res = array(‘error‘ => 1, ‘message‘ => ‘未知错误‘);
 }

echo json_encode($res);

二、文件图片的展示[kindeditor中filemanager]


 $ns = new Zend_Session_Namespace(‘DB‘);
 $cid = $ns->cid;
 $baseDir = $this->uploadPath . $cid;

 $imageExts = array(‘jpg‘ => ‘.jpg‘, ‘png‘ => ‘.png‘, ‘gif‘ => ‘.gif‘);
 $onlyImage = $this->getQuery(‘dir‘) == ‘image‘;
 $list = array();
 if (is_dir($baseDir)) {
     $dir = new DirectoryIterator($baseDir);
     foreach ($dir as $fileinfo) {
         if ($fileinfo->isFile()) {
             $isPhoto = isset($imageExts[$fileinfo->getExtension()]);
             if ($onlyImage && !$isPhoto) {
                 continue;
             }
         $filename = $fileinfo->getFilename();
         $args = array($this->token, $cid, $filename);
         sort($args, SORT_STRING);
         $sign = sha1(implode(‘‘, $args));
         $list[] = array(
             ‘uri‘ => ‘cid=‘ . $cid . ‘&file=‘ . urlencode($filename) . ‘&sign=‘ . $sign,
             ‘filename‘ => $filename,
             ‘filesize‘ => $fileinfo->getSize(),
             ‘datetime‘ => date(‘Y-m-d H:i:s‘, $fileinfo->getMTime()),
             ‘is_photo‘ => $isPhoto
         );
     }
 }
 }

if ($list && isset($_GET[‘order‘])) {
     switch ($_GET[‘order‘]) {
     case ‘NAME‘:
         usort($list, function ($a, $b) {
             return strcmp($a[‘filename‘], $b[‘filename‘]);
         });
     break;
     case ‘SIZE‘:
         usort($list, function ($a, $b) {
             if ($a[‘filesize‘] > $b[‘filesize‘]) {
                 return 1;
             } elseif ($a[‘filesize‘] < $b[‘filesize‘]) {
                 return -1;
             }
             return 0;
         });
     break;
     case ‘DATE‘:
         usort($list, function ($a, $b) {
             return strcmp($a[‘datetime‘], $b[‘datetime‘]);
         });
         break;
     }
 }

 echo json_encode(array(
     ‘base_url‘ => $this->filesUrl . ‘?‘,
     ‘file_list‘ => $list
 ));

kindeditor 本地图片文件上传

标签:

原文地址:http://my.oschina.net/yueguanqun/blog/397302

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!