upfileChajian.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. 上传文件类upfileChajian
  4. create:chenxihu
  5. createdt:2013-11-15
  6. explain:上传文件类,可上传任何文件类型
  7. */
  8. class upfileChajian extends Chajian{
  9. public $ext; //上传类型
  10. public $maxsize; //上传大小(MB)
  11. public $path; //文件夹
  12. private $jpgallext = '|jpg|png|gif|bmp|jpeg|'; //图片格式
  13. //可上传文件类型,也就是不保存为uptemp的文件
  14. private $upallfile = '|doc|docx|xls|xlsx|ppt|pptx|pdf|swf|rar|zip|txt|gz|wav|mp3|avi|mp4|flv|wma|chm|apk|amr|log|json|cdr|psd|caf|wps|';
  15. /**
  16. 初始化
  17. @param $ext string 上传类型
  18. @param $path string 上传目录 如:upload|e|ee
  19. @param $maxsize ing 上传大小(MB)
  20. */
  21. public function initupfile($ext,$path,$maxsize=1)
  22. {
  23. if($ext=='image')$ext = $this->jpgallext;
  24. $this->ext = $ext;
  25. $this->maxsize = $maxsize;
  26. $this->path = $path;
  27. }
  28. private function _getmaxupsize($lx)
  29. {
  30. $iniMax = strtolower(ini_get($lx));
  31. if ('' === $iniMax) {
  32. return PHP_INT_MAX;
  33. }
  34. $max = ltrim($iniMax, '+');
  35. if (0 === strpos($max, '0x')) {
  36. $max = intval($max, 16);
  37. } elseif (0 === strpos($max, '0')) {
  38. $max = intval($max, 8);
  39. } else {
  40. $max = (int) $max;
  41. }
  42. switch (substr($iniMax, -1)) {
  43. case 't': $max *= 1024;
  44. case 'g': $max *= 1024;
  45. case 'm': $max *= 1024;
  46. case 'k': $max *= 1024;
  47. }
  48. return $max;
  49. }
  50. public function getmaxupsize()
  51. {
  52. $post = $this->_getmaxupsize('post_max_size');
  53. $upmx = $this->_getmaxupsize('upload_max_filesize');
  54. if($post < $upmx)$upmx = $post;
  55. return $upmx;
  56. }
  57. public function getmaxzhao()
  58. {
  59. $size = $this->getmaxupsize();
  60. $size = $size / 1024 / 1024;
  61. return (int)$size;
  62. }
  63. /**
  64. * 是否在可保存范围内容
  65. */
  66. public function issavefile($ext)
  67. {
  68. $bo = false;
  69. $upallfile = $this->jpgallext.$this->upallfile;
  70. if($this->contain($upallfile, '|'.$ext.'|'))$bo = true;
  71. $stype = getconfig('savefiletype');//可直接保存的文件类型
  72. if($stype && !$bo && $this->contain(','.$stype.',', ','.$ext.','))$bo = true;
  73. return $bo;
  74. }
  75. public function isimg($ext)
  76. {
  77. return $this->contain($this->jpgallext, '|'.$ext.'|');
  78. }
  79. /**
  80. * 判断是不是图片
  81. */
  82. public function isimgsave($ext, $file)
  83. {
  84. $arr = array();
  85. if(!file_exists($file))return $arr;
  86. if($this->isimg($ext)){
  87. list($picw,$pich) = getimagesize($file);
  88. if($picw==0||$pich==0){
  89. @unlink($file);
  90. }
  91. $arr[0] = $picw;
  92. $arr[1] = $pich;
  93. }
  94. return $arr;
  95. }
  96. public function isoffice($ext)
  97. {
  98. return contain('|doc|docx|xls|xlsx|ppt|pptx|pdf|', '|'.$ext.'|');
  99. }
  100. /**
  101. 上传
  102. @param $name string 对应文本框名称
  103. @param $cfile string 文件名心的文件名,不带扩展名的
  104. @return string/array
  105. */
  106. public function up($name,$cfile='')
  107. {
  108. if(!$_FILES)return 'sorry!';
  109. $file_name = $_FILES[$name]['name'];
  110. $file_size = $_FILES[$name]['size'];//字节
  111. $file_type = $_FILES[$name]['type'];
  112. $file_error = $_FILES[$name]['error'];
  113. $file_tmp_name = $_FILES[$name]['tmp_name'];
  114. $zongmax = $this->getmaxupsize();
  115. if($file_size<=0 || $file_size > $zongmax){
  116. return '文件为0字节/超过'.$this->formatsize($zongmax).',不能上传';
  117. }
  118. $file_sizecn = $this->formatsize($file_size);
  119. $file_ext = $this->getext($file_name);//文件扩展名
  120. $file_img = $this->isimg($file_ext);
  121. $file_kup = $this->issavefile($file_ext);
  122. if(!$file_img && !$this->isoffice($file_ext) && getconfig('systype')=='demo')return '演示站点禁止文件上传';
  123. if($file_error>0){
  124. $rrs = $this->geterrmsg($file_error);
  125. return $rrs;
  126. }
  127. if(!$this->contain('|'.$this->ext.'|', '|'.$file_ext.'|') && $this->ext != '*'){
  128. return '禁止上传文件类型['.$file_ext.']';
  129. }
  130. if($file_size>$this->maxsize*1024*1024){
  131. return '上传文件过大,限制在:'.$this->formatsize($this->maxsize*1024*1024).'内,当前文件大小是:'.$file_sizecn.'';
  132. }
  133. //创建目录
  134. $zpath=explode('|',$this->path);
  135. $mkdir='';
  136. for($i=0;$i<count($zpath);$i++){
  137. $mkdir.=''.$zpath[$i].'/';
  138. if(!is_dir($mkdir))mkdir($mkdir);
  139. }
  140. //新的文件名
  141. $file_newname = $file_name;
  142. $randname = $file_name;
  143. if(!$cfile==''){
  144. $file_newname=''.$cfile.'.'.$file_ext.'';
  145. }else{
  146. $_oldval = m('option')->getval('randfilename');
  147. $randname = $this->getrandfile(1, $_oldval);
  148. m('option')->setval('randfilename', $randname);
  149. $file_newname=''.$randname.'.'.$file_ext.'';
  150. }
  151. $save_path = ''.str_replace('|','/',$this->path);
  152. //if(!is_writable($save_path))return '目录'.$save_path.'无法写入不能上传';
  153. $allfilename= $save_path.'/'.$file_newname.'';
  154. $uptempname = $save_path.'/'.$randname.'.uptemp';
  155. $upbool = true;
  156. if(!$file_kup){
  157. $allfilename= $this->filesave($file_tmp_name, $file_newname, $save_path, $file_ext);
  158. if(isempt($allfilename))return '无法保存到'.$save_path.'';
  159. }else{
  160. $upbool = @move_uploaded_file($file_tmp_name,$allfilename);
  161. }
  162. if($upbool){
  163. $picw=0;$pich=0;
  164. if($file_img){
  165. $fobj = $this->isimgsave($file_ext, $allfilename);
  166. if(!$fobj){
  167. return 'error:非法图片文件';
  168. }else{
  169. $picw = $fobj[0];
  170. $pich = $fobj[1];
  171. }
  172. }
  173. return array(
  174. 'newfilename' => $file_newname,
  175. 'oldfilename' => $file_name,
  176. 'filesize' => $file_size,
  177. 'filesizecn' => $file_sizecn,
  178. 'filetype' => $file_type,
  179. 'filepath' => $save_path,
  180. 'fileext' => $file_ext,
  181. 'allfilename' => $allfilename,
  182. 'picw' => $picw,
  183. 'pich' => $pich
  184. );
  185. }else{
  186. return '上传失败:'.$this->geterrmsg($file_error).'';
  187. }
  188. }
  189. private function getrandfile($xu, $val)
  190. {
  191. $randname = ''.date('d_His').''.rand(10,99*$xu).'';
  192. if($val==$randname)return $this->getrandfile($xu+1, $val);
  193. return $randname;
  194. }
  195. private function geterrmsg($code)
  196. {
  197. $arrs[1] = '上传文件大小超过服务器允许上传的最大值';
  198. $arrs[2] = '上传文件大小超过HTML表单中隐藏域MAX_FILE_SIZE选项指定的值';
  199. $arrs[6] = '没有找不到临时文件夹';
  200. $arrs[7] = '文件写入失败';
  201. $arrs[8] = 'php文件上传扩展没有打开';
  202. $arrs[3] = '文件只有部分被上传';
  203. $rrs = '上传失败,可能是服务器内部出错,请重试';
  204. if(isset($arrs[$code]))$rrs=$arrs[$code];
  205. return $rrs;
  206. }
  207. //返回文件大小
  208. public function formatsize($size)
  209. {
  210. return $this->rock->formatsize($size);
  211. }
  212. //获取扩展名
  213. public function getext($file)
  214. {
  215. return strtolower(substr($file,strrpos($file,'.')+1));
  216. }
  217. /**
  218. * 非法文件保存为临时uptemp的形式
  219. */
  220. public function filesave($oldfile, $filename, $savepath, $ext)
  221. {
  222. $file_kup = $this->issavefile($ext);
  223. $ldisn = strrpos($filename, '.');
  224. if($ldisn>0)$filename = substr($filename, 0, $ldisn);
  225. $filepath = ''.$savepath.'/'.$filename.'.'.$ext.'';
  226. if(!$file_kup){
  227. $filebase64 = base64_encode(file_get_contents($oldfile));
  228. $filepath = ''.$savepath.'/'.$filename.'.uptemp';
  229. $bo = $this->rock->createtxt($filepath, $filebase64);
  230. @unlink($oldfile);
  231. if(!$bo)$filepath = '';
  232. }else{
  233. }
  234. return $filepath;
  235. }
  236. }
粤ICP备19079148号