UploadedFile.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. class UploadedFile
  3. {
  4. static private $_files;
  5. private $_name;
  6. private $_tempName;
  7. private $_type;
  8. private $_size;
  9. private $_error;
  10. static $_this;
  11. /**
  12. * 获取实例
  13. *
  14. * @param 你上传的key
  15. * @return 返回该类的一些属性,见下面的构造函数
  16. * 如果没有文件上传,返回的是null
  17. */
  18. public static function getInstanceByName($name)
  19. {
  20. if(null===self::$_files)
  21. self::prefetchFiles();
  22. return isset(self::$_files[$name]) && self::$_files[$name]->getError()!=UPLOAD_ERR_NO_FILE ? self::$_files[$name] : null;
  23. }
  24. /**
  25. * 遍历出 $_FILES 的元素属性
  26. *
  27. */
  28. protected static function prefetchFiles()
  29. {
  30. self::$_files = array();
  31. if(!isset($_FILES) || !is_array($_FILES))
  32. return;
  33. foreach($_FILES as $class=>$info)
  34. self::collectFilesRecursive($class, $info['name'], $info['tmp_name'], $info['type'], $info['size'], $info['error']);
  35. }
  36. /**
  37. * @param string $key FILE的key
  38. * @param mixed $names 文件名
  39. * @param mixed $tmp_names 临时文件名
  40. * @param mixed $types 文件类型
  41. * @param mixed $sizes 文件大小
  42. * @param mixed $errors 错误码
  43. */
  44. protected static function collectFilesRecursive($key, $names, $tmp_names, $types, $sizes, $errors)
  45. {
  46. if(is_array($names))
  47. {
  48. foreach($names as $item=>$name)
  49. self::collectFilesRecursive($key.'['.$item.']', $names[$item], $tmp_names[$item], $types[$item], $sizes[$item], $errors[$item]);
  50. }
  51. else
  52. self::$_files[$key] = new UploadedFile($names, $tmp_names, $types, $sizes, $errors);
  53. }
  54. /**
  55. * 构造函数.
  56. * @param string $name 上传时候的文件名
  57. * @param string $tempName 临时文件
  58. * @param string $type 文件类型 (如 "image/gif").
  59. * @param integer $size 文件大小
  60. * @param integer $error 错误码
  61. */
  62. public function __construct($name,$tempName,$type,$size,$error)
  63. {
  64. $this->_name=$name;
  65. $this->_tempName=$tempName;
  66. $this->_type=$type;
  67. $this->_size=$size;
  68. $this->_error=$error;
  69. self::$_this = $this;
  70. }
  71. public function __toString()
  72. {
  73. return $this->_name;
  74. }
  75. /**
  76. * 保存文件
  77. * @param string $file 保存文件的地方
  78. * @param boolean $deleteTempFile 上传成功后,是否删除
  79. * @return boolean
  80. */
  81. public static function saveAs($file,$deleteTempFile=true)
  82. {
  83. if(self::$_this->_error==UPLOAD_ERR_OK) {
  84. if($deleteTempFile) {
  85. return move_uploaded_file(self::$_this->_tempName,$file);
  86. }
  87. elseif(is_uploaded_file(self::$_this->_tempName)) {
  88. return copy(self::$_this->_tempName, $file);
  89. }
  90. else {
  91. return false;
  92. }
  93. }
  94. else {
  95. return false;
  96. }
  97. }
  98. public function getName()
  99. {
  100. return $this->_name;
  101. }
  102. public function getTempName()
  103. {
  104. return $this->_tempName;
  105. }
  106. public function getType()
  107. {
  108. return $this->_type;
  109. }
  110. public function getSize()
  111. {
  112. return $this->_size;
  113. }
  114. public function getError()
  115. {
  116. return $this->_error;
  117. }
  118. public function getHasError()
  119. {
  120. return $this->_error!=UPLOAD_ERR_OK;
  121. }
  122. public function getExtensionName()
  123. {
  124. if(($pos=strrpos($this->_name,'.'))!==false)
  125. return (string)substr($this->_name,$pos+1);
  126. else
  127. return '';
  128. }
  129. }