discuz 附件上传按版块自动创建目录-Discuz教程下载

discuz 附件上传按版块自动创建目录

开通本站Svip会员,全站资源免费下
目录source/class/Discuz/discuz_upload.php
  1. <?php
  2. /**
  3. *      [Discuz!] (C)2001-2099 Comsenz Inc.
  4. *      This is NOT a freeware, use is subject to license terms
  5. *
  6. *      $Id: discuz_upload.php 34648 2014-06-18 02:53:07Z hypowang $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9.         exit('Access Denied');
  10. }
  11. Class discuz_upload{
  12.         var $attach = array();
  13.         var $type = '';
  14.         var $extid = 0;
  15.         var $errorcode = 0;
  16.         var $forcename = '';
  17.         public function __construct() {
  18.         }
  19.         function init($attach, $type = 'temp', $extid = 0, $forcename = '') {
  20.                 if(!is_array($attach) || empty($attach) || !$this->is_upload_file($attach['tmp_name']) || trim($attach['name']) == '' || $attach['size'] == 0) {
  21.                         $this->attach = array();
  22.                         $this->errorcode = -1;
  23.                         return false;
  24.                 } else {
  25.                         $this->type = $this->check_dir_type($type);
  26.                         $this->extid = intval($extid);
  27.                         $this->forcename = $forcename;
  28.                         $attach['size'] = intval($attach['size']);
  29.                         $attach['name'] =  trim($attach['name']);
  30.                         $attach['thumb'] = '';
  31.                         $attach['ext'] = $this->fileext($attach['name']);
  32.                         $attach['name'] =  dhtmlspecialchars($attach['name'], ENT_QUOTES);
  33.                         if(strlen($attach['name']) > 90) {
  34.                                 $attach['name'] = cutstr($attach['name'], 80, '').'.'.$attach['ext'];
  35.                         }
  36.                         $attach['isimage'] = $this->is_image_ext($attach['ext']);
  37.                         $attach['extension'] = $this->get_target_extension($attach['ext']);
  38.                         $attach['attachdir'] = $this->get_target_dir($this->type, $extid);
  39.                         $attach['attachment'] = $attach['attachdir'].$this->get_target_filename($this->type, $this->extid, $this->forcename).'.'.$attach['extension'];
  40.                         $attach['target'] = getglobal('setting/attachdir').'./'.$this->type.'/'.$attach['attachment'];
  41.                         $this->attach = & $attach;
  42.                         $this->errorcode = 0;
  43.                         return true;
  44.                 }
  45.         }
  46.         function save($ignore = 0) {
  47.                 if($ignore) {
  48.                         if(!$this->save_to_local($this->attach['tmp_name'], $this->attach['target'])) {
  49.                                 $this->errorcode = -103;
  50.                                 return false;
  51.                         } else {
  52.                                 $this->errorcode = 0;
  53.                                 return true;
  54.                         }
  55.                 }
  56.                 if(empty($this->attach) || empty($this->attach['tmp_name']) || empty($this->attach['target'])) {
  57.                         $this->errorcode = -101;
  58.                 } elseif(in_array($this->type, array('group', 'album', 'category')) && !$this->attach['isimage']) {
  59.                         $this->errorcode = -102;
  60.                 } elseif(in_array($this->type, array('common')) && (!$this->attach['isimage'] && $this->attach['ext'] != 'ext')) {
  61.                         $this->errorcode = -102;
  62.                 } elseif(!$this->save_to_local($this->attach['tmp_name'], $this->attach['target'])) {
  63.                         $this->errorcode = -103;
  64.                 } elseif(($this->attach['isimage'] || $this->attach['ext'] == 'swf') && (!$this->attach['imageinfo'] = $this->get_image_info($this->attach['target'], true))) {
  65.                         $this->errorcode = -104;
  66.                         @unlink($this->attach['target']);
  67.                 } else {
  68.                         $this->errorcode = 0;
  69.                         return true;
  70.                 }
  71.                 return false;
  72.         }
  73.         function error() {
  74.                 return $this->errorcode;
  75.         }
  76.         function errormessage() {
  77.                 return lang('error', 'file_upload_error_'.$this->errorcode);
  78.         }
  79.         function fileext($filename) {
  80.                 return addslashes(strtolower(substr(strrchr($filename, '.'), 1, 10)));
  81.         }
  82.         function is_image_ext($ext) {
  83.                 static $imgext  = array('jpg', 'jpeg', 'gif', 'png', 'bmp');
  84.                 return in_array($ext, $imgext) ? 1 : 0;
  85.         }
  86.         function get_image_info($target, $allowswf = false) {
  87.                 $ext = discuz_upload::fileext($target);
  88.                 $isimage = discuz_upload::is_image_ext($ext);
  89.                 if(!$isimage && ($ext != 'swf' || !$allowswf)) {
  90.                         return false;
  91.                 } elseif(!is_readable($target)) {
  92.                         return false;
  93.                 } elseif($imageinfo = @getimagesize($target)) {
  94.                         list($width, $height, $type) = !empty($imageinfo) ? $imageinfo : array('', '', '');
  95.                         $size = $width * $height;
  96.                         if($size > 16777216 || $size < 16 ) {
  97.                                 return false;
  98.                         } elseif($ext == 'swf' && $type != 4 && $type != 13) {
  99.                                 return false;
  100.                         } elseif($isimage && !in_array($type, array(1,2,3,6,13))) {
  101.                                 return false;
  102.                         } elseif(!$allowswf && ($ext == 'swf' || $type == 4 || $type == 13)) {
  103.                                 return false;
  104.                         }
  105.                         return $imageinfo;
  106.                 } else {
  107.                         return false;
  108.                 }
  109.         }
  110.         function is_upload_file($source) {
  111.                 return $source && ($source != 'none') && (is_uploaded_file($source) || is_uploaded_file(str_replace('\\\\', '\\', $source)));
  112.         }
  113.         function get_target_filename($type, $extid = 0, $forcename = '') {
  114.                 if($type == 'group' || ($type == 'common' && $forcename != '')) {
  115.                         $filename = $type.'_'.intval($extid).($forcename != '' ? "_$forcename" : '');
  116.                 } else {
  117.                         $filename = date('His').strtolower(random(16));
  118.                 }
  119.                 return $filename;
  120.         }
  121.         function get_target_extension($ext) {
  122.                 static $safeext  = array('attach', 'jpg', 'jpeg', 'gif', 'png', 'swf', 'bmp', 'txt', 'zip', 'rar', 'mp3');
  123.                 return strtolower(!in_array(strtolower($ext), $safeext) ? 'attach' : $ext);
  124.         }
  125.         function get_target_dir($type, $extid = '', $check_exists = true) {
  126.                 $subdir = $subdir1 = $subdir2 = '';
  127.                 if($type == 'album' || $type == 'forum' || $type == 'portal' || $type == 'category' || $type == 'profile') {
  128.                         $subdir1 = date('Ym');
  129.                         $subdir2 = date('d');
  130.                         $subdir = $subdir1.'/'.$subdir2.'/';
  131.                 } elseif($type == 'group' || $type == 'common') {
  132.                         $subdir = $subdir1 = substr(md5($extid), 0, 2).'/';
  133.                 }
  134.                 $check_exists && discuz_upload::check_dir_exists($type, $subdir1, $subdir2);
  135.                 $res = $_SERVER['HTTP_REFERER'];
  136.                 $urlinfo = parse_url($res);
  137.                 $query_str = $urlinfo['query'];
  138.                 $query_arr =$this->getUrlQuery($query_str);
  139.                 if(isset($query_arr['fid'])){
  140.                         $fid = $query_arr['fid'];
  141.                         $mod = new table_forum_forum();
  142.                         $modinfo = $mod->get_forum_by_fid($fid);
  143.                         $name = $modinfo['name'];
  144.                         $subdir .= $name.'/';
  145.                         discuz_upload::check_dir_exists($type, $subdir1.'/'.$subdir2,$name);
  146.                 }
  147.                 return $subdir;
  148.         }
  149.         function getUrlQuery($array_query)
  150.         {
  151.                 $arr = explode('&',$array_query);
  152.                 $tmp = array();
  153.                 foreach($arr as $k)
  154.                 {
  155.                         $temp_arr = explode('=',$k);
  156.                         if(count($temp_arr) == 2){
  157.                                 $k = $temp_arr[0];
  158.                                 $v = $temp_arr[1];
  159.                                 $tem[$k] = $v;
  160.                         }
  161.                 }
  162.                 return $tem;
  163.         }
  164.         function check_dir_type($type) {
  165.                 return !in_array($type, array('forum', 'group', 'album', 'portal', 'common', 'temp', 'category', 'profile')) ? 'temp' : $type;
  166.         }
  167.         function check_dir_exists($type = '', $sub1 = '', $sub2 = '') {
  168.                 $type = discuz_upload::check_dir_type($type);
  169.                 $basedir = !getglobal('setting/attachdir') ? (DISCUZ_ROOT.'./data/attachment') : getglobal('setting/attachdir');
  170.                 $typedir = $type ? ($basedir.'/'.$type) : '';
  171.                 $subdir1  = $type && $sub1 !== '' ?  ($typedir.'/'.$sub1) : '';
  172.                 $subdir2  = $sub1 && $sub2 !== '' ?  ($subdir1.'/'.$sub2) : '';
  173.                 $res = $subdir2 ? is_dir($subdir2) : ($subdir1 ? is_dir($subdir1) : is_dir($typedir));
  174.                 if(!$res) {
  175.                         $res = $typedir && discuz_upload::make_dir($typedir);
  176.                         $res && $subdir1 && ($res = discuz_upload::make_dir($subdir1));
  177.                         $res && $subdir1 && $subdir2 && ($res = discuz_upload::make_dir($subdir2));
  178.                 }
  179.                 return $res;
  180.         }
  181.         function save_to_local($source, $target) {
  182.                 if(!discuz_upload::is_upload_file($source)) {
  183.                         $succeed = false;
  184.                 }elseif(@copy($source, $target)) {
  185.                         $succeed = true;
  186.                 }elseif(function_exists('move_uploaded_file') && @move_uploaded_file($source, $target)) {
  187.                         $succeed = true;
  188.                 }elseif (@is_readable($source) && (@$fp_s = fopen($source, 'rb')) && (@$fp_t = fopen($target, 'wb'))) {
  189.                         while (!feof($fp_s)) {
  190.                                 $s = @fread($fp_s, 1024 * 512);
  191.                                 @fwrite($fp_t, $s);
  192.                         }
  193.                         fclose($fp_s); fclose($fp_t);
  194.                         $succeed = true;
  195.                 }
  196.                 if($succeed)  {
  197.                         $this->errorcode = 0;
  198.                         @chmod($target, 0644); @unlink($source);
  199.                 } else {
  200.                         $this->errorcode = 0;
  201.                 }
  202.                 return $succeed;
  203.         }
  204.         function make_dir($dir, $index = true) {
  205.                 $res = true;
  206.                 if(!is_dir($dir)) {
  207.                         $res = @mkdir($dir, 0777);
  208.                         $index && @touch($dir.'/index.html');
  209.                 }
  210.                 return $res;
  211.         }
  212. }
复制代码

全部评论 0

您需要登录后才可以回帖 立即登录
登录
0
0
0
返回顶部