destfolder = $custdir; // 自定义上传的文件路径 $this->uppath = $custpath; $this->upname = $this->setname ( $custname ); $this->uptypes = $custtypes; $this->upmaxsize = $custmaxsize; $this->filefield = ( string ) $filefield; $this->time = time (); // 初始化主要用来统一文件名称和目录 } /** * 重命名上传文件,支持中文名 * * @param string $custname * @return string */ private function setname($custname) { return ! empty ( $custname ) ? iconv ( utf-8, gbk, $custname ) : date ( 'ymdhis', $this->time ) . mt_rand ( 10, 99 ); /* * if ($custname == '') { // 如果未设置文件名,则生成一个随机文件名 $name = date ( * 'ymdhis',$this->time ) . _ . mt_rand ( 10, 99 ) . '.' . $this->ext; * // 判断文件是否存在,不允许重复文件 if (file_exists ( $this->savepath . $name )) { * $name = setsavename ( $savename ); } } else { $name = $savename; } * $this->savename = $name; } */ } private function setpath() { return (preg_match ( '/\/$/', $this->uppath )) ? $this->uppath : $this->uppath . '/'; } /** * 创建目录 * * @param string $basedir * @param string $destdir */ private function mkdirs($basedir, $destdir) { $dirs = $basedir; ! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 ); // 原来如果前面的假是正的。后面的语句就执行 if (! empty ( $destdir )) { $destdirs = explode ( '/', $destdir ); foreach ( $destdirs as $finaldir ) { ! empty ( $finaldir ) && $dirs .= $finaldir . '/'; ! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 ); } } else { $dirs .= date ( 'ymd', $this->time ) . '/'; ! is_dir ( $dirs ) && @mkdir ( $dirs, 0777 ); } return $dirs; } /** * 获得后缀函数 * * @param string $filename * @return mixed */ private function getfileext($filename) { $extend = pathinfo ( $filename ); $this->chkfileext = $extend ['extension']; } /** * 检测文件后缀函数 * * @return boolean */ private function checkfileext() { if (in_array ( $this->chkfileext, $this->uptypes )) { // 此处程序有bug return true; } else { $this->uperror = 1; return false; } } /** * 检测最大尺寸 * * @return boolean */ private function checkfilemaxsize() { if ($this->chkfilesize > $this->upmaxsize) { $this->uperror = 2; return false; } return true; } /* * (non-phpdoc) @see upload::fileupload() */ public function fileupload() { // 单文件、多文件上传 $keys = array_keys ( $_files [$this->filefield] ['name'] ); foreach ( $keys as $key ) { if (! $_files [$this->filefield] ['name'] [$key]) continue; $syserror = $_files [$this->filefield] ['error'] [$key]; switch ($syserror) { case 1 : $this->uperror = 3; break; case 2 : $this->uperror = 4; break; case 3 : $this->uperror = 5; break; case 4 : $this->uperror = 6; break; case 5 : $this->uperror = 7; break; } $this->chkfilename = iconv ( utf-8, gbk, $_files [$this->filefield] ['name'] [$key] ); // 循环中的文件名称 $this->chkfilesize = $_files [$this->filefield] ['size'] [$key]; // 循环中的文件大小 $this->getfileext ( $this->chkfilename ); // 文件类型检测 if (! $this->checkfileext ()) { return $this->errmsg (); exit (); } // 超过大小 if (! $this->checkfilemaxsize ()) { return $this->errmsg (); exit (); } if ($syserror == 0 && is_uploaded_file ( $_files [$this->filefield] ['tmp_name'] [$key] )) { // 组装文件名称 /* * $upfullpathname = $this->upname . $key . '.' . * $this->chkfileext; // 不允许重复 if (file_exists ( $upfullpathname * )) { $this->upfullname = $upfullpathname; } */ $this->upfullname = $this->upname . $key . '.' . $this->chkfileext; $this->upfullpathname = $this->mkdirs ( $this->destfolder, $this->setpath () ) . $this->upfullname; if (move_uploaded_file ( $_files [$this->filefield] ['tmp_name'] [$key], $this->upfullpathname )) { $this->sucssinfo ['name'] = $this->upfullpathname; $this->sucssinfo ['size'] = $this->chkfilesize; $this->sucssinfo ['info'] = '文件' . $this->upfullname . '上传成功!'; } } } return $this->sucssinfo; } /* * (non-phpdoc) @see upload::errmsg() */ public function errmsg() { $errmsg = array ( 0 => '文件上传成功!', 1 => '上传文件' . $this->chkfilename . '类型错误,只支持上传' . implode ( ',', $this->uptypes ) . '等文件类型!', 2 => '上传文件' . $this->chkfilename . '太大,最大支持' . ceil ( $this->upmaxsize / 1024 ) . 'kb的文件', 3 => '上传文件' . $this->chkfilename . '超过了 php.ini 中 upload_max_filesize 选项限制的值。', 4 => '上传文件' . $this->chkfilename . '大小超过了 html 表单中 max_file_size 选项指定的值!', 5 => '文件' . $this->chkfilename . '只有部分被上传!', 6 => '没有文件被上传。', 7 => '文件上传失败!' ); if ($this->uperror == 0) return false; else return $errmsg [$this->uperror]; }}
发布下自己定义的上传类!环境新人和高手拍砖!
# custom define page if( access_has_global_level( config_get( 'manage_site_threshold' ) ) ) { $t_menu_options[] = '' ) . lang_get( 'custorm_list_link' ) . ''; $t_menu_options[] = '' ) . lang_get( 'ot_link' ) . ''; $t_menu_options[] = '' ) . lang_get( 'payment_link' ) . ''; } # contact list if( !current_user_is_anonymous() ) { $t_menu_options[] = '' ) . lang_get( 'contact_list_link' ) . ''; }