最新消息: 新版网站上线了!!!

thinkphp3.2 kindeditor上传图片的控制器方法

  1.  public function file() 
  2.     { 
  3.         $baseUrl = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])); 
  4.         import('ORG.Net.UploadFile'); 
  5.         import('ORG.Util.Services_JSON'); 
  6.         $upload = new UploadFile(); 
  7.         $upload->maxSize = 3145728; 
  8.         $upload->allowExts = array('jpg''gif''png''jpeg'); 
  9.         $upload->savePath = './uploads/Images/'
  10.         $info = $upload->uploadOne($_FILES['imgFile']); 
  11.         $file_url = $baseUrl . 'uploads/Images/' . $info['0']['savename']; 
  12.         if ($info) { 
  13.             header('Content-type: text/html; charset=UTF-8'); 
  14.             $json = new Services_JSON(); 
  15.             echo $json->encode(array('error' => 0, 'url' => $file_url)); 
  16.             exit
  17.         } else { 
  18.             $this->error($upload->getErrorMsg()); 
  19.         } 
  20.     } 
  21.  
  22.     public function file_manager() 
  23.     { 
  24.         import('ORG.Util.Services_JSON'); 
  25.         $php_path = dirname(__FILE__) . '/'
  26.         $php_url = dirname($_SERVER['PHP_SELF']) . '/'
  27.         $root_path = $php_path . './uploads/Images/'
  28.         $root_url = $php_url . './uploads/Images/'
  29.         $ext_arr = array('gif''jpg''jpeg''png''bmp'); 
  30.         $dir_name = emptyempty($_GET['dir']) ? '' : trim($_GET['dir']); 
  31.         if (!in_array($dir_namearray('''image''flash''media''file'))) { 
  32.             echo "Invalid Directory name."
  33.             exit
  34.         } 
  35.         if ($dir_name !== '') { 
  36.             $root_path .= $dir_name . "/"
  37.             $root_url .= $dir_name . "/"
  38.             if (!file_exists($root_path)) { 
  39.                 mkdir($root_path); 
  40.             } 
  41.         } 
  42. //根据path参数,设置各路径和URL 
  43.         if (emptyempty($_GET['path'])) { 
  44.             $current_path = realpath($root_path) . '/'
  45.             $current_url = $root_url
  46.             $current_dir_path = ''
  47.             $moveup_dir_path = ''
  48.         } else { 
  49.             $current_path = realpath($root_path) . '/' . $_GET['path']; 
  50.             $current_url = $root_url . $_GET['path']; 
  51.             $current_dir_path = $_GET['path']; 
  52.             $moveup_dir_path = preg_replace('/(.*?)[^\/]+\/$/''$1'$current_dir_path); 
  53.         } 
  54.         echo realpath($root_path); 
  55. //排序形式,name or size or type 
  56.         $order = emptyempty($_GET['order']) ? 'name' : strtolower($_GET['order']); 
  57.  
  58. //不允许使用..移动到上一级目录 
  59.         if (preg_match('/\.\./'$current_path)) { 
  60.             echo 'Access is not allowed.'
  61.             exit
  62.         } 
  63. //最后一个字符不是/ 
  64.         if (!preg_match('/\/$/'$current_path)) { 
  65.             echo 'Parameter is not valid.'
  66.             exit
  67.         } 
  68. //目录不存在或不是目录 
  69.         if (!file_exists($current_path) || !is_dir($current_path)) { 
  70.             echo 'Directory does not exist.'
  71.             exit
  72.         } 
  73.  
  74. //遍历目录取得文件信息 
  75.         $file_list = array(); 
  76.         if ($handle = opendir($current_path)) { 
  77.             $i = 0; 
  78.             while (false !== ($filename = readdir($handle))) { 
  79.                 if ($filename{0} == '.'continue
  80.                 $file = $current_path . $filename
  81.                 if (is_dir($file)) { 
  82.                     $file_list[$i]['is_dir'] = true; //是否文件夹 
  83.                     $file_list[$i]['has_file'] = (count(scandir($file)) > 2); //文件夹是否包含文件 
  84.                     $file_list[$i]['filesize'] = 0; //文件大小 
  85.                     $file_list[$i]['is_photo'] = false; //是否图片 
  86.                     $file_list[$i]['filetype'] = ''//文件类别,用扩展名判断 
  87.                 } else { 
  88.                     $file_list[$i]['is_dir'] = false; 
  89.                     $file_list[$i]['has_file'] = false; 
  90.                     $file_list[$i]['filesize'] = filesize($file); 
  91.                     $file_list[$i]['dir_path'] = ''
  92.                     $file_ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); 
  93.                     $file_list[$i]['is_photo'] = in_array($file_ext$ext_arr); 
  94.                     $file_list[$i]['filetype'] = $file_ext
  95.                 } 
  96.                 $file_list[$i]['filename'] = $filename//文件名,包含扩展名 
  97.                 $file_list[$i]['datetime'] = date('Y-m-d H:i:s'filemtime($file)); //文件最后修改时间 
  98.                 $i++; 
  99.             } 
  100.             closedir($handle); 
  101.         } 
  102.  
  103. //排序 
  104.  
  105.         usort($file_list'cmp_func'); 
  106.  
  107.         $result = array(); 
  108. //相对于根目录的上一级目录 
  109.         $result['moveup_dir_path'] = $moveup_dir_path
  110. //相对于根目录的当前目录 
  111.         $result['current_dir_path'] = $current_dir_path
  112. //当前目录的URL 
  113.         $result['current_url'] = $current_url
  114. //文件数 
  115.         $result['total_count'] = count($file_list); 
  116. //文件列表数组 
  117.         $result['file_list'] = $file_list
  118.  
  119. //输出JSON字符串 
  120.         header('Content-type: application/json; charset=UTF-8'); 
  121.         $json = new Services_JSON(); 
  122.         echo $json->encode($result); 
  123.     } 

 

转载请注明:谷谷点程序 » thinkphp3.2 kindeditor上传图片的控制器方法