+ -
当前位置:首页 → 问答吧 → 非常实用的上传类,上传效果在线演示

非常实用的上传类,上传效果在线演示

时间:2010-01-01

来源:互联网

复制代码
  1. #*********************************************************
  2. #文件名称:
  3. inc_class.upload.php
  4. #Copyright (c)
  5. 2007-2009 青春一度 all rights reserved.
  6. #最后更新:     2009-08-05
  7. #版本    :     v 2.0.a
  8. #注:转发时请保留此声明信息,这段声明不并会影响你的速度!
  9. #如有修改请将修改后的文件以邮件形式发送给作者一份,谢谢!
  10. #
  11. #*********************************************************
  12. if(!defined('IN_PHPADLEYIU'))
  13. {
  14. exit('Access Denied');
  15. }
  16. /*
  17. //使用说明:
  18. //声明一个上传类
  19. include_once(ADLEYLIU_ROOT.'./inc_class.upload.php');
  20. $_YL_UPLOAD
  21. = array();
  22. $yl_uploadfile = new
  23. yl_upload_class();
  24. $_YL_UPLOAD['yl_filedata'] =
  25. 'uploadFile';//表单名
  26. $_YL_UPLOAD['yl_directroy'] =
  27. 'upload_files';//上传主目录
  28. $_YL_UPLOAD['file_urldirectroy'] = '/';//
  29. 程序路径
  30. $_YL_UPLOAD['yl_settingsnew'] =
  31. ''.date('ym').'/'.date('d').'/'.substr(time(), 0,
  32. 5).'';//上传子主目录
  33. $_YL_UPLOAD['yl_maxsize'] = 1048576; 
  34. //这里以字节为单位(1024*2)*1024=2097152 就是 2M
  35. $_YL_UPLOAD['yl_sizeformat'] =
  36. 'k';   //显示文件大小单位b字节,k千,m兆
  37. $_YL_UPLOAD['yl_arrext'] =
  38. array('gif','jpg','jpeg','png','bmp','rar','txt');//允许上传文件类型
  39. $_YL_UPLOAD['yl_ext'] = 0;  //0原文件类型上传,1统一为存为jpg
  40. $_YL_UPLOAD['yl_prefix'] = ''.$uid.''.$yl_uploadfile -> yl_createrand(1,0).''.$cid.''; 
  41. //在文件名前缀加上特殊字符 //$uid 会员ID  $cid 分类ID
  42. $_YL_UPLOAD['yl_suffix'] = '';  //''.$yl_uploadfile -> yl_createrand(3,0).''; 
  43. //在文件名后缀加上特殊字符
  44. $_YL_UPLOAD['thumbwidth'] = 100; 
  45. //缩略图宽
  46. $_YL_UPLOAD['thumbheight'] = 100; 
  47. //缩略图高
  48. $_YL_UPLOAD['maxthumbwidth'] = 500; 
  49. //大图高
  50. $_YL_UPLOAD['maxthumbheight'] = 500; 
  51. //大图宽
  52. //上传
  53. $yl_uploadfile -> yl_uploadfile();
  54. 获取值:
  55. 'yl_filename' => addslashes($_YL_UPLOAD['yl_filename']),原文件名
  56. 'yl_attachment' => $_YL_UPLOAD['yl_attachment'],新文件名及路径
  57. 'yl_filesize' => $_YL_UPLOAD['yl_filesize'] ,文件大小
  58. 'yl_filetype' => $_YL_UPLOAD['yl_filetype'],文件类型
  59. 'yl_isimage' => $_YL_UPLOAD['yl_isimage'],是否是图片
  60. 'yl_isthumb' => $_YL_UPLOAD['yl_isthumb'],是否有小图片
  61. */
  62. class yl_upload_class
  63. {
  64. function __GET($property_name)
  65. {
  66.   if(isset($this -> $property_name))
  67. {
  68.    return $this -> $property_name;
  69.   } else
  70. {
  71.    return
  72. NULL;
  73.   }
  74. }
  75. function __SET($property_name,
  76. $value) {
  77.   $this -> $property_name =
  78. $value;
  79. }
  80. #*********************************************************
  81. #生成缩略图
  82. #*********************************************************
  83. function makethumb($srcfile) {
  84.   global $_YL_UPLOAD;
  85.   //判断文件是否存在
  86.   if (!file_exists($srcfile))
  87. {
  88.    return '';
  89.   }
  90.   $dstfile =
  91. $srcfile.'.small.jpg';
  92.  
  93.   $bigfile =
  94. $srcfile.'.big.jpg';
  95.   //缩略图大小
  96.   $tow =
  97. intval($_YL_UPLOAD['thumbwidth']);
  98.   $toh =
  99. intval($_YL_UPLOAD['thumbheight']);
  100.   if($tow < 60) $tow =
  101. 60;
  102.   if($toh < 60) $toh = 60;
  103.   $make_max = 0;
  104.   $maxtow =
  105. intval($_YL_UPLOAD['maxthumbwidth']);
  106.   $maxtoh =
  107. intval($_YL_UPLOAD['maxthumbheight']);
  108.   if($maxtow >= 300
  109. && $maxtoh >= 300) {
  110.    $make_max =
  111. 1;
  112.   }
  113.   //获取图片信息
  114.   $im = '';
  115.   if($data =
  116. getimagesize($srcfile)) {
  117.    if($data[2] == 1)
  118. {
  119.     $make_max =
  120. 0;//gif不处理
  121.     if(function_exists("imagecreatefromgif"))
  122. {
  123.      $im =
  124. imagecreatefromgif($srcfile);
  125.     }
  126.    }
  127. elseif($data[2] == 2)
  128. {
  129.     if(function_exists("imagecreatefromjpeg"))
  130. {
  131.      $im =
  132. imagecreatefromjpeg($srcfile);
  133.     }
  134.    }
  135. elseif($data[2] == 3)
  136. {
  137.     if(function_exists("imagecreatefrompng"))
  138. {
  139.      $im =
  140. imagecreatefrompng($srcfile);
  141.     }
  142.    }
  143.   }
  144.   if(!$im)
  145. return '';
  146.   $srcw = imagesx($im);
  147.   $srch = imagesy($im);
  148.   $towh = $tow/$toh;
  149.   $srcwh =
  150. $srcw/$srch;
  151.   if($towh <= $srcwh){
  152.    $ftow =
  153. $tow;
  154.    $ftoh = $ftow*($srch/$srcw);
  155.    $fmaxtow = $maxtow;
  156.    $fmaxtoh =
  157. $fmaxtow*($srch/$srcw);
  158.   } else {
  159.    $ftoh =
  160. $toh;
  161.    $ftow = $ftoh*($srcw/$srch);
  162.    $fmaxtoh = $maxtoh;
  163.    $fmaxtow =
  164. $fmaxtoh*($srcw/$srch);
  165.   }
  166.   if($srcw <= $maxtow
  167. && $srch <= $maxtoh) {
  168.    $make_max =
  169. 0;//不处理
  170.   }
  171.   if($srcw > $tow || $srch > $toh)
  172. {
  173.    if(function_exists("imagecreatetruecolor") &&
  174. function_exists("imagecopyresampled") && @$ni =
  175. imagecreatetruecolor($ftow, $ftoh))
  176. {
  177.     imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow,
  178. $ftoh, $srcw,
  179. $srch);
  180.     //大图片
  181.     if($make_max
  182. && @$maxni = imagecreatetruecolor($fmaxtow, $fmaxtoh))
  183. {
  184.      imagecopyresampled($maxni, $im, 0, 0, 0, 0,
  185. $fmaxtow, $fmaxtoh, $srcw, $srch);
  186.     }else if (@$maxni
  187. = imagecreatetruecolor(round($srcw/2),
  188. round($srch/2))){
  189.      imagecopyresampled($maxni,
  190. $im, 0, 0, 0, 0, round($srcw/2), round($srch/2), $srcw,
  191. $srch);
  192.     }
  193.    }
  194. elseif(function_exists("imagecreate") &&
  195. function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh))
  196. {
  197.     imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow,
  198. $ftoh, $srcw,
  199. $srch);
  200.     //大图片
  201.     if($make_max
  202. && @$maxni = imagecreate($fmaxtow, $fmaxtoh))
  203. {
  204.      imagecopyresized($maxni, $im, 0, 0, 0, 0,
  205. $fmaxtow, $fmaxtoh, $srcw, $srch);
  206.     }else if (@$maxni
  207. = imagecreate(round($srcw/2),
  208. round($srch/2))){
  209.      imagecopyresized($maxni, $im,
  210. 0, 0, 0, 0, round($srcw/2), round($srch/2), $srcw,
  211. $srch);
  212.     }
  213.    } else
  214. {
  215.     return
  216. '';
  217.    }
  218.    if(function_exists('imagejpeg'))
  219. {
  220.     imagejpeg($ni,
  221. $dstfile);
  222.     //大图片
  223.     if($make_max)
  224. {
  225.      imagejpeg($maxni,
  226. $bigfile);
  227.     }else{
  228.      imagejpeg($maxni,
  229. $bigfile);
  230.     }
  231.    }
  232. elseif(function_exists('imagepng')) {
  233.     imagepng($ni,
  234. $dstfile);
  235.     //大图片
  236.     if($make_max)
  237. {
  238.      imagepng($maxni,
  239. $bigfile);
  240.     }else{
  241.      imagejpeg($maxni,
  242. $bigfile);
  243.     }
  244.    }
  245.    imagedestroy($ni);
  246.    if($make_max)
  247. {
  248.    }else{
  249.     imagedestroy($maxni);
  250.    }
  251.   }else{
  252.           
  253. if(function_exists("imagecreatetruecolor") &&
  254. function_exists("imagecopyresampled") && @$ni =
  255. imagecreatetruecolor($srcw, $srch))
  256. {
  257.     imagecopyresampled($ni, $im, 0, 0, 0, 0, $srcw,
  258. $ftoh, $srch,
  259. $srch);
  260.     //大图片
  261.      $maxni =
  262. imagecreatetruecolor($srch, $srch);
  263.     
  264. imagecopyresampled($maxni, $im, 0, 0, 0, 0, $srcw, $srch, $srcw,
  265. $srch);
  266.    } elseif(function_exists("imagecreate")
  267. && function_exists("imagecopyresized") && @$ni =
  268. imagecreate($ftow, $ftoh)) {
  269.     imagecopyresized($ni,
  270. $im, 0, 0, 0, 0, $srcw, $srch, $srcw,
  271. $srch);
  272.     //大图片
  273.      $maxni =
  274. imagecreate($fmaxtow, $fmaxtoh);
  275.     
  276. imagecopyresized($maxni, $im, 0, 0, 0, 0, $srcw, $srch, $srcw,
  277. $srch);
  278.    
  279.    } else
  280. {
  281.     return
  282. '';
  283.    }
  284.                 
  285. imagejpeg($ni, $dstfile);
  286.      imagejpeg($maxni,
  287. $bigfile);
  288.  
  289.   }
  290.   imagedestroy($im);
  291.   if(!file_exists($dstfile)) {
  292.    return
  293. '';
  294.   } else {
  295.    return
  296. $dstfile;
  297.   }
  298. }
  299. #*********************************************************
  300. #获取随机数函数
  301. #*********************************************************
  302. function
  303. yl_createrand($length, $numeric = 0) {
  304.   PHP_VERSION < '4.2.0'
  305. && mt_srand((double)microtime() * 1000000);
  306.   if($numeric)
  307. {
  308.    $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10,
  309. $length) - 1));
  310.   } else {
  311.    $hash =
  312. '';
  313.    $chars =
  314. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';///0123456789
  315.    $max
  316. = strlen($chars) - 1;
  317.    for($i = 0; $i < $length; $i++)
  318. {
  319.     $hash .= $chars[mt_rand(0,
  320. $max)];
  321.    }
  322.   }
  323.   return
  324. $hash;
  325. }
  326. #***************
  327. #*********************************************************
  328. #创建目录函数
  329. #*********************************************************
  330. function
  331. createfolder($yl_path)
  332. {
  333.   if
  334. (!file_exists($yl_path))
  335.   {
  336.    $this ->
  337. createfolder(dirname($yl_path));
  338.    @mkdir($yl_path,
  339. 0777);
  340.   }
  341.   return $this ->
  342. createfolder;
  343. }
  344. #*********************************************************
  345. #获取文件
  346. 名称,大小,类型,临时文件名
  347. #*********************************************************
  348. function
  349. yl_getfilename($yl_type)
  350. {
  351.   global
  352. $_YL_UPLOAD;
  353.   return
  354. $_FILES[$_YL_UPLOAD['yl_filedata']][$yl_type];
  355. }
  356. #*********************************************************
  357. #获取文件大小
  358. #*********************************************************
  359. function
  360. yl_getfilesize()
  361. {
  362.   global
  363. $_YL_UPLOAD;
  364.   $yl_filesize = $this ->
  365. yl_getfilename('size');
  366.   if($yl_filesize ==
  367. 0){
  368.    $this ->
  369. alert("请选择上传文件!");
  370.    exit;
  371.   }
  372.   if($yl_filesize
  373. > $_YL_UPLOAD['yl_maxsize']){
  374.    switch
  375. (strtolower($_YL_UPLOAD['yl_sizeformat'])){
  376.     case
  377. 'b':
  378.      $yl_maxsizek = $_YL_UPLOAD['yl_maxsize'] .
  379. ' B';
  380.      break;
  381.     case
  382. 'k':
  383.      $yl_maxsizek =
  384. $_YL_UPLOAD['yl_maxsize']/1024 . '
  385. K';
  386.      break;
  387.     case
  388. 'm':
  389.      $yl_maxsizek =
  390. $_YL_UPLOAD['yl_maxsize']/(1024*1024) . '
  391. M';
  392.    }
  393.    $this ->
  394. alert("上传文件超出限制范围[".$yl_maxsizek."].K!");
  395.    exit;
  396.   }
  397.   return
  398. $yl_filesize;
  399. }
  400. #*********************************************************
  401. #获得文件扩展名
  402. #*********************************************************
  403. function
  404. yl_getfiletype()
  405. {
  406.   global
  407. $_YL_UPLOAD;
  408.   $pathinfo = pathinfo($this -> yl_getfilename('name'));
  409.   $yl_file_ext =
  410. strtolower($pathinfo['extension']);
  411.   //检查扩展名
  412.   if(!array_keys($_YL_UPLOAD['yl_arrext'],$yl_file_ext))
  413. {
  414.    $this ->
  415. alert("上传文件类型被限制!");
  416.    exit;
  417.   }
  418.   return
  419. $yl_file_ext;
  420. }
  421. #*********************************************************
  422. #上传验证
  423. #*********************************************************
  424. function
  425. yl_upfile($source, $target) {
  426.   //
  427. 如果一种函数上传失败,还可以用其他函数上传
  428.   if (function_exists('move_uploaded_file')
  429. && @move_uploaded_file($source, $target))
  430. {
  431.    @chmod($target, 0666);
  432.    return
  433. $target;
  434.   } elseif (@copy($source, $target))
  435. {
  436.    @chmod($target, 0666);
  437.    return
  438. $target;
  439.   } elseif (@is_readable($source))
  440. {
  441.    if ($fp = @fopen($source,'rb'))
  442. {
  443.     @flock($fp,2);
  444.     $filedata
  445. =
  446. @fread($fp,@filesize($source));
  447.     @fclose($fp);
  448.    }
  449.    if
  450. ($fp = @fopen($target, 'wb')) {
  451.     @flock($fp,
  452. 2);
  453.     @fwrite($fp,
  454. $filedata);
  455.     @fclose($fp);
  456.     @chmod
  457. ($target, 0666);
  458.     return
  459. $target;
  460.    } else {
  461.     return
  462. false;
  463.    }
  464.   }
  465. }
  466. #*********************************************************
  467. #上传
  468. #*********************************************************
  469. function
  470. yl_uploadfile()
  471. {
  472.   global $_YL_UPLOAD;
  473.   $yl_file_path = $_YL_UPLOAD['yl_directroy'].'/'.$_YL_UPLOAD['yl_settingsnew'] ;//建立一个目录
  474.   $yl_filename = $this -> yl_getfilename('name');//原文件名
  475.   $yl_filenamenews = $_YL_UPLOAD['yl_prefix'].''.substr(time(), 5, 9).''.$_YL_UPLOAD['yl_suffix'].'';//重命名
  476.   $yl_file_size = $this -> yl_getfilesize();//获取文件大小
  477.   $yl_file_type = $this -> yl_getfiletype();//获取文件类型
  478.   if($_YL_UPLOAD['yl_ext'] ==
  479. 0){
  480.    $yl_filenamenewsext = $yl_filenamenews.'.'.$yl_file_type;//改名
  481.   }elseif ($_YL_UPLOAD['yl_ext'] == 1){
  482.    $yl_filenamenewsext = $yl_filenamenews.'.jpg';//统一改名为jpg
  483.   }
  484.   //$yl_tmp_name =  str_replace(' ','',$this ->
  485. yl_getfilename('tmp_name'));//服务器上临时文件名
  486.   $yl_tmp_name =  $this -> yl_getfilename('tmp_name');//服务器上临时文件名
  487.   //检查是否已上传
  488.   if(<A
  489. href="[email=!@is_uploaded_file($yl_tmp_name]!@is_uploaded_file($yl_tmp_name">mailto:!@is_uploaded_file($yl_tmp_name">!@is_uploaded_file($yl_tmp_name[/email]))
  490. {
  491.    $this ->
  492. alert("文件已上传!");
  493.    exit;
  494.   }
  495.   //检查目录是否存在,不存在则创建
  496.   if(<A
  497. href="[email=!@is_dir(]!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">mailto:!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">!@is_dir(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''[/email]))
  498. {
  499.    $this ->
  500. createfolder(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.'');//创建目录
  501.   }
  502.   //检查目录写权限
  503.   if
  504. (<A
  505. href="[email=!@is_writable(]!@is_writable(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">mailto:!@is_writable(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''">!@is_writable(''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.''[/email]))
  506. {
  507.    $this ->
  508. alert("上传目录没有写权限!");
  509.    exit;
  510.   }
  511.   $yl_path_name
  512. =
  513. ''.$_YL_UPLOAD['file_urldirectroy'].''.$yl_file_path.'/'.$yl_filenamenewsext.'';
  514.   $yl_doupload = $this -> yl_upfile($yl_tmp_name, $yl_path_name);
  515.   if($yl_doUpload === false){
  516.    $this -> alert("上传失败!");
  517.    exit;
  518.   }else{
  519.    //echo
  520. '上传成功';
  521.    //echo
  522. '<br>';
  523.    /*
  524.    echo
  525. '原文件名:'.$yl_filename.'';
  526.    echo
  527. '<br>';
  528.    echo
  529. '新文件名及目录:'.$yl_file_path.'/'.$yl_filenamenewsext;
  530.    echo
  531. '<br>';
  532.    echo
  533. '文件大小:'.$yl_file_size.'';
  534.    echo '<br>';
  535.    echo '文件类型:'.$yl_file_type.'';
  536.    */
  537.    $_YL_UPLOAD['yl_filename'] = $yl_filename;
  538.    $_YL_UPLOAD['yl_attachment'] = ''.$yl_file_path.'/'.$yl_filenamenewsext.'';
  539.    $_YL_UPLOAD['yl_filesize'] = $yl_file_size;
  540.    $_YL_UPLOAD['yl_filetype'] = $yl_file_type;
  541.    //检查是否图片
  542.    if(@getimagesize($yl_path_name))
  543. {
  544.     $_YL_UPLOAD['yl_isimage'] =
  545. 1;
  546.     ///生成缩略图
  547.     if ($this -> makethumb($yl_path_name)){
  548.      $_YL_UPLOAD['yl_isthumb'] = 1;
  549.     }else{
  550.      $_YL_UPLOAD['yl_isthumb'] = 0;
  551.     }
  552.    }else{
  553.     $_YL_UPLOAD['yl_isimage'] = 0;
  554.    }
  555.   }
  556.   return
  557. true;
  558. }
  559. #*********************************************************
  560. #提示
  561. #*********************************************************
  562. function
  563. alert($yl_msg)
  564. {
  565.   echo '<html>';
  566.   echo '<head>';
  567.   echo '<title>error</title>';
  568.   echo '<meta http-equiv="content-type" content="text/html; charset=gb2312">';
  569.   echo '</head>';
  570.   echo '<body>';
  571.   echo '<script type="text/javascript">alert("'.$yl_msg.'");history.back();</script>';
  572.   echo '</body>';
  573.   echo '</html>';
  574.   exit;
  575. }
  576. }

作者: 我要公仔   发布时间: 2010-01-01

演示地址?

作者: ^淡如清风   发布时间: 2010-01-01

在线演示???

作者: xujing3344   发布时间: 2010-01-01

吓。。

作者: 刹那芳华网   发布时间: 2010-01-03

在哪看呢??

作者: chensen   发布时间: 2010-01-03

难道虚拟演示?

作者: aoyoo   发布时间: 2010-01-04

莫非我眼花了~~

作者: 雅典娜之子   发布时间: 2010-01-04

演示地址呢?

作者: chensen   发布时间: 2010-01-04

额滴神啊,这个上传类还挺长

作者: redpower8   发布时间: 2010-01-04

没演示的?!

作者: ccczzz   发布时间: 2010-02-02