zoukankan      html  css  js  c++  java
  • php面向对象文件上传

    文件上传类 Upload.class.php
    1. <?php
    2. class Upload {
    3. private $path;
    4. private $n;
    5. private $ext;
    6. private $length;
    7. public function __construct($path = './', $n = 3, $ext = ['jpg','gif','png','rar','ppt','doc'], $length = 20971520) {
    8. $this->path = $path;
    9. $this->n = $n;
    10. $this->ext = $ext;
    11. $this->length = $length;
    12. }
    13. public function up() {
    14. $ufs = $_FILES;
    15. if (count ( $ufs ) <= 0) {
    16. return;
    17. } else {
    18. if (! file_exists ( $this->path )) {
    19. mkdir ( $this->path, 0777, true );
    20. }
    21. $uuu = [ ];
    22. foreach ( $ufs as $k => $v ) {
    23. if (is_array ( $v ['name'] )) {
    24. $ns = $v ['name'];
    25. $fs = $v ['tmp_name'];
    26. $ss = $v ['size'];
    27. foreach ( $fs as $kk => $vv ) {
    28. if ($ss [$kk] == 0) {
    29. continue;
    30. }
    31. $uf = iconv ( 'utf-8', 'gbk', $ns [$kk] );
    32. $et = strtolower ( substr ( $uf, strrpos ( $uf, '.' ) + 1 ) );
    33. if ($this->n == 1) {
    34. $uf = uniqid () . '.' . $et;
    35. } else if ($this->n == 2) {
    36. $uf = $this->uuid () . '.' . $et;
    37. } else if ($this->n == 3) {
    38. $nnn = substr ( $uf, 0, strrpos ( $uf, '.' ) );
    39. $t = 0;
    40. while ( file_exists ( $this->path . $uf ) ) {
    41. $uf = $nnn . (++ $t) . '.' . $et;
    42. }
    43. }
    44. if (in_array ( $et, $this->ext ) && $ss [$kk] <= $this->length) {
    45. move_uploaded_file ( $vv, $this->path . $uf );
    46. $uuu [] = $uf;
    47. }
    48. }
    49. } else {
    50. if ($v ['size'] == 0) {
    51. continue;
    52. }
    53. $uf = iconv ( 'utf-8', 'gbk', $v ['name'] );
    54. $et = strtolower ( substr ( $uf, strrpos ( $uf, '.' ) + 1 ) );
    55. if ($this->n == 1) {
    56. $uf = uniqid () . '.' . $et;
    57. } else if ($this->n == 2) {
    58. $uf = $this->uuid () . '.' . $et;
    59. } else if ($this->n == 3) {
    60. $nnn = substr ( $uf, 0, strrpos ( $uf, '.' ) );
    61. $t = 0;
    62. while ( file_exists ( $this->path . $uf ) ) {
    63. $uf = $nnn . (++ $t) . '.' . $et;
    64. }
    65. }
    66. if (in_array ( $et, $this->ext ) && $v ['size'] <= $this->length) {
    67. move_uploaded_file ( $v ['tmp_name'], $this->path . $uf );
    68. $uuu [] = $uf;
    69. }
    70. }
    71. }
    72. return $uuu;
    73. }
    74. }
    75. public function uuid($namespace = '') {
    76. $guid = $namespace;
    77. $uid = uniqid ( "", true );
    78. $data = $namespace;
    79. $data .= $_SERVER ['REQUEST_TIME'];
    80. $data .= $_SERVER ['HTTP_USER_AGENT'];
    81. $data .= $_SERVER ['REMOTE_ADDR'];
    82. $data .= $_SERVER ['REMOTE_PORT'];
    83. $hash = strtoupper ( hash ( 'ripemd128', $uid . $guid . md5 ( $data ) ) );
    84. $guid = substr ( $hash, 0, 8 ) . '-' . substr ( $hash, 8, 4 ) . '-' . substr ( $hash, 12, 4 ) . '-' . substr ( $hash, 16, 4 ) . '-' . substr ( $hash, 20, 12 );
    85. return $guid;
    86. }
    87. public function __destruct() {
    88. }
    89. }
    上传单个文件或者多个文件的html代码  uup.html
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Insert title here</title>
    6. </head>
    7. <body>
    8. <form action="up.php" method="post" enctype="multipart/form-data">
    9. 文件上传:<input type="file" name="f[]" multiple><br><!-- 上传多个文件 -->
    10. <!-- 上传文件:<input type="file" name="f" ><br> 上传单个文件 -->
    11. <input type="submit" value="提交">
    12. </form>
    13. </body>
    14. </html>
    引入类和实例化类的代码up.php
    1. <?php
    2. include 'inc/Upload.class.php';
    3. $u=new Upload('abc/',1);
    4. $uf=$u->up();
    5. echo '<pre>';
    6. print_r($uf);






  • 相关阅读:
    Eclipse 远程调试
    大数据处理方法bloom filter
    sicily 1259 Sum of Consecutive Primes
    sicily 1240. Faulty Odometer
    sicily 1152 简单马周游 深度优先搜索及回溯算法
    sicily 1050 深度优先搜索解题
    sicily 1024 邻接矩阵与深度优先搜索解题
    sicily 1156 二叉树的遍历 前序遍历,递归,集合操作
    sicily 1443 队列基本操作
    sicily 1006 team rankings 枚举解题
  • 原文地址:https://www.cnblogs.com/lsr111/p/4594291.html
Copyright © 2011-2022 走看看