Kindeditor使用方法及びThinkPHP画像アップロード

50731 ワード

KindEditorの使い方とThinkPHP画像のアップロード
KindEditor使用
ダウンロード
HTMLコード
textareaを定義する
<textarea id='content' ></textarea>

jsコード:
 var editor=KindEditor.create('textarea#product_category_content',{ allowFileManager:true,width:683,height:380, uploadJson:_app_+'/rbac/user/upload' });

ThinkPHPアップロード
ThinkPHP/Library/Org/net/でUploadFileを増加する.class.php
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2009 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: liu21st <[email protected]> // +---------------------------------------------------------------------- namespace Org\Net; /** *       * @category ORG * @package ORG * @subpackage Net * @author liu21st <[email protected]> */ class UploadFile {//      private $config = array( 'maxSize' => -1, //          'supportMulti' => true, //           'allowExts' => array(), //                    'allowTypes' => array(), //                  'thumb' => false, //                'imageClassPath' => 'ORG.Util.Image', //        'thumbMaxWidth' => '',//         'thumbMaxHeight' => '',//         'thumbPrefix' => 'thumb_',//       'thumbSuffix' => '', 'thumbPath' => '',//         'thumbFile' => '',//        'thumbExt' => '',//         'thumbRemoveOrigin' => false,//        'zipImages' => false,//          'autoSub' => false,//           'subType' => 'hash',//             hash date custom 'subDir' => '', //       subType custom      'dateFormat' => 'Ymd', 'hashLevel' => 1, // hash      'savePath' => '',//          'autoCheck' => true, //          'uploadReplace' => false,//          'saveRule' => 'uniqid',//          'hashType' => 'md5_file',//     Hash      ); //      private $error = ''; //           private $uploadFileInfo ; public function __get($name){ if(isset($this->config[$name])) { return $this->config[$name]; } return null; } public function __set($name,$value){ if(isset($this->config[$name])) { $this->config[$name] = $value; } } public function __isset($name){ return isset($this->config[$name]); } /** *      * @access public * @param array $config      */ public function __construct($config=array()) { if(is_array($config)) { $this->config = array_merge($this->config,$config); } } /** *        * @access public * @param mixed $name    * @param string $value      * @return string */ private function save($file) { $filename = $file['savepath'].$file['savename']; if(!$this->uploadReplace && is_file($filename)) { //         $this->error = '      !'.$filename; return false; } //                if( in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png','swf'))) { $info = getimagesize($file['tmp_name']); if(false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))){ $this->error = '      '; return false; } } if(!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) { $this->error = '        !'; return false; } if($this->thumb && in_array(strtolower($file['extension']),array('gif','jpg','jpeg','bmp','png'))) { $image = getimagesize($filename); if(false !== $image) { //           $thumbWidth = explode(',',$this->thumbMaxWidth); $thumbHeight = explode(',',$this->thumbMaxHeight); $thumbPrefix = explode(',',$this->thumbPrefix); $thumbSuffix = explode(',',$this->thumbSuffix); $thumbFile = explode(',',$this->thumbFile); $thumbPath = $this->thumbPath?$this->thumbPath:dirname($filename).'/'; $thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension']; //          //         import($this->imageClassPath); for($i=0,$len=count($thumbWidth); $i<$len; $i++) { if(!empty($thumbFile[$i])) { $thumbname = $thumbFile[$i]; }else{ $prefix = isset($thumbPrefix[$i])?$thumbPrefix[$i]:$thumbPrefix[0]; $suffix = isset($thumbSuffix[$i])?$thumbSuffix[$i]:$thumbSuffix[0]; $thumbname = $prefix.basename($filename,'.'.$file['extension']).$suffix; } Image::thumb($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true); } if($this->thumbRemoveOrigin) { //             unlink($filename); } } } if($this->zipImags) { // TODO            } return true; } /** *        * @access public * @param string $savePath          * @return string */ public function upload($savePath ='') { //          ,       if(empty($savePath)) $savePath = $this->savePath; //        if(!is_dir($savePath)) { //            if(is_dir(base64_decode($savePath))) { $savePath = base64_decode($savePath); }else{ //        if(!mkdir($savePath)){ $this->error = '    '.$savePath.'   '; return false; } } }else { if(!is_writeable($savePath)) { $this->error = '    '.$savePath.'   '; return false; } } $fileInfo = array(); $isUpload = false; //           //  $_FILES       $files = $this->dealFiles($_FILES); foreach($files as $key => $file) { //        if(!empty($file['name'])) { //            if(!isset($file['key'])) $file['key'] = $key; $file['extension'] = $this->getExt($file['name']); $file['savepath'] = $savePath; $file['savename'] = $this->getSaveName($file); //        if($this->autoCheck) { if(!$this->check($file)) return false; } //       if(!$this->save($file)) return false; if(function_exists($this->hashType)) { $fun = $this->hashType; $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk')); } //           ,        unset($file['tmp_name'],$file['error']); $fileInfo[] = $file; $isUpload = true; } } if($isUpload) { $this->uploadFileInfo = $fileInfo; return true; }else { $this->error = '        '; return false; } } /** *                    * @access public * @param array $file        * @param string $savePath          * @return string */ public function uploadOne($file,$savePath=''){ //          ,       if(empty($savePath)) $savePath = $this->savePath; //        if(!is_dir($savePath)) { //        if(!mkdir($savePath,0777,true)){ $this->error = '    '.$savePath.'   '; return false; } }else { if(!is_writeable($savePath)) { $this->error = '    '.$savePath.'   '; return false; } } //        if(!empty($file['name'])) { $fileArray = array(); if(is_array($file['name'])) { $keys = array_keys($file); $count = count($file['name']); for ($i=0; $i<$count; $i++) { foreach ($keys as $key) $fileArray[$i][$key] = $file[$key][$i]; } }else{ $fileArray[] = $file; } $info = array(); foreach ($fileArray as $key=>$file){ //            $file['extension'] = $this->getExt($file['name']); $file['savepath'] = $savePath; $file['savename'] = $this->getSaveName($file); //        if($this->autoCheck) { if(!$this->check($file)) return false; } //       if(!$this->save($file)) return false; if(function_exists($this->hashType)) { $fun = $this->hashType; $file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk')); } unset($file['tmp_name'],$file['error']); $info[] = $file; } //           return $info; }else { $this->error = '        '; return false; } } /** *                  * @access private * @param array $files         * @return array */ private function dealFiles($files) { $fileArray = array(); $n = 0; foreach ($files as $key=>$file){ if(is_array($file['name'])) { $keys = array_keys($file); $count = count($file['name']); for ($i=0; $i<$count; $i++) { $fileArray[$n]['key'] = $key; foreach ($keys as $_key){ $fileArray[$n][$_key] = $file[$_key][$i]; } $n++; } }else{ $fileArray[$key] = $file; } } return $fileArray; } /** *          * @access public * @param string $errorNo      * @return void */ protected function error($errorNo) { switch($errorNo) { case 1: $this->error = '         php.ini   upload_max_filesize       '; break; case 2: $this->error = '           HTML     MAX_FILE_SIZE       '; break; case 3: $this->error = '         '; break; case 4: $this->error = '       '; break; case 6: $this->error = '        '; break; case 7: $this->error = '      '; break; default: $this->error = '      !'; } return ; } /** *                   * @access private * @param string $filename    * @return string */ private function getSaveName($filename) { $rule = $this->saveRule; if(empty($rule)) {//        ,         $saveName = $filename['name']; }else { if(function_exists($rule)) { //                $saveName = $rule().".".$filename['extension']; }else { //              $saveName = $rule.".".$filename['extension']; } } if($this->autoSub) { //           $filename['savename'] = $saveName; $saveName = $this->getSubName($filename).$saveName; } return $saveName; } /** *          * @access private * @param array $file         * @return string */ private function getSubName($file) { switch($this->subType) { case 'custom': $dir = $this->subDir; break; case 'date': $dir = date($this->dateFormat,time()).'/'; break; case 'hash': default: $name = md5($file['savename']); $dir = ''; for($i=0;$i<$this->hashLevel;$i++) { $dir .= $name{$i}.'/'; } break; } if(!is_dir($file['savepath'].$dir)) { mkdir($file['savepath'].$dir,0777,true); } return $dir; } /** *         * @access private * @param array $file      * @return boolean */ private function check($file) { if($file['error']!== 0) { //       //       $this->error($file['error']); return false; } //      ,          //       if(!$this->checkSize($file['size'])) { $this->error = '        !'; return false; } //    Mime   if(!$this->checkType($file['type'])) { $this->error = '    MIME     !'; return false; } //       if(!$this->checkExt($file['extension'])) { $this->error ='         '; return false; } //         if(!$this->checkUpload($file['tmp_name'])) { $this->error = '      !'; return false; } return true; } //                private function autoCharset($fContents, $from='gbk', $to='utf-8') { $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from; $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to; if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) { //                   return $fContents; } if (function_exists('mb_convert_encoding')) { return mb_convert_encoding($fContents, $to, $from); } elseif (function_exists('iconv')) { return iconv($from, $to, $fContents); } else { return $fContents; } } /** *               * @access private * @param string $type    * @return boolean */ private function checkType($type) { if(!empty($this->allowTypes)) return in_array(strtolower($type),$this->allowTypes); return true; } /** *               * @access private * @param string $ext     * @return boolean */ private function checkExt($ext) { if(!empty($this->allowExts)) return in_array(strtolower($ext),$this->allowExts,true); return true; } /** *            * @access private * @param integer $size    * @return boolean */ private function checkSize($size) { return !($size > $this->maxSize) || (-1 == $this->maxSize); } /** *            * @access private * @param string $filename     * @return boolean */ private function checkUpload($filename) { return is_uploaded_file($filename); } /** *           * @access private * @param string $filename     * @return boolean */ private function getExt($filename) { $pathinfo = pathinfo($filename); return $pathinfo['extension']; } /** *           * @access public * @return array */ public function getUploadFileInfo() { return $this->uploadFileInfo; } /** *            * @access public * @return string */ public function getErrorMsg() { return $this->error; } }

新しいrestメソッド:
 public function upload(){
        if(!isset($this->U)){
            return array('result'=>'Timeout');
        }
        // import('Org.Net.UploadFile');
        $upload = new \Org\Net\UploadFile();
        //        
        //$upload->maxSize = 3292200;
        //        
        $upload->allowExts = explode(',', 'png,jpg');
        //        
        $upload->savePath = './Uploads/';
        if (!$upload->upload()) {
          //      
          //$this->error($upload->getErrorMsg());
          $this->response(array("result"=>"Fail"),'json');
        } else {
          //           
          $uploadList = $upload->getUploadFileInfo();
          $savename = $uploadList[0]['savename'];

          $this->response(array('error'=>0,"url"=>'/Uploads/'.$savename ),'json');
        }   
      }

注意点
uploadサービス側から返されるデータが正しい場合は、「error」=>0を割り当てる必要があります.
そうでなければ、アップロード後にエラーウィンドウがポップアップされ、ウィンドウの内容には「message」=>」属性値が表示されます.