Uploader.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * The Feeler framework, licensed under LGPL license
  4. * Author: Rick Kuo
  5. */
  6. class Uploader{
  7. public $dir;
  8. public $path;
  9. public $maxSize;
  10. public $force;
  11. public $allowTypes = "*";
  12. public $checkTypes = array();
  13. public $fieldsMappings = array();
  14. private $_sysErrCodeBaseNum = 1000;
  15. private $_commonErrCodeBaseNum = 2000;
  16. function __construct($dir, $maxSize = 8096, $force = false){
  17. $dir .= date("Y/m/d/");
  18. $this->dir = $dir;
  19. $this->path = Yii::app()->basePath."/../".$dir;
  20. $this->maxSize = $maxSize * 1024;
  21. $this->force = $force;
  22. }
  23. private function _getErrorCode($errCode, $sys = false){
  24. if(is_numeric($errCode) && $errCode){
  25. if($sys) $errCode += $this->_sysErrCodeBaseNum;
  26. else $errCode += $this->_commonErrCodeBaseNum;
  27. }
  28. return $errCode;
  29. }
  30. public function act(){
  31. $filesInfo = array();
  32. File::mkdir($this->path);
  33. foreach($_FILES as $field => $file){
  34. $fileInfo = array();
  35. if(!isset($_FILES[$field]["name"]) || $_FILES[$field]["name"] === "")
  36. continue;
  37. if(is_array($_FILES[$field]["name"])){
  38. $keys = array_keys($_FILES[$field]["name"]);
  39. foreach($keys as $key){
  40. $filesInfo[$field][$key]["name"] = $_FILES[$field]["name"][$key];
  41. $filesInfo[$field][$key]["error"] = $this->_getErrorCode($_FILES[$field]["error"][$key], true);
  42. if($filesInfo[$field][$key]["error"] == 0){
  43. $fileExt = File::getExt($_FILES[$field]["name"][$key]);
  44. if($fileExt == null){
  45. $fileExt = File::getType($_FILES[$field]["tmp_name"][$key]);
  46. }
  47. if($this->allowTypes !== "*"){
  48. if(!is_array($this->allowTypes)){
  49. $filesInfo[$field][$key]["error"] = $this->_getErrorCode(1);
  50. }
  51. else if(is_array($fileExt) && !array_intersect($fileExt, $this->allowTypes)){
  52. $filesInfo[$field][$key]["error"] = $this->_getErrorCode(1);
  53. }
  54. else if(!is_array($fileExt) && !in_array($fileExt, $this->allowTypes)){
  55. $filesInfo[$field][$key]["error"] = $this->_getErrorCode(1);
  56. }
  57. }
  58. else if($_FILES[$field]["size"][$key] > $this->maxSize){
  59. $filesInfo[$field][$key]["error"] = $this->_getErrorCode(2);
  60. }
  61. if($filesInfo[$field][$key]["error"] == 0 && is_uploaded_file($_FILES[$field]["tmp_name"][$key])){
  62. if(is_array($fileExt))
  63. $fileExt = current($fileExt);
  64. $filesInfo[$field][$key]["md5"] = md5_file($_FILES[$field]["tmp_name"][$key]);
  65. $filesInfo[$field][$key]["name"] = isset($this->fieldsMappings[$field][$key]) ? $this->fieldsMappings[$field][$key].".".$fileExt : $filesInfo[$field][$key]["md5"].".".$fileExt;
  66. $filesInfo[$field][$key]["ext"] = $fileExt;
  67. $filesInfo[$field][$key]["path"] = $this->path;
  68. $filesInfo[$field][$key]["dir"] = $this->dir;
  69. $filesInfo[$field][$key]["src"] = $this->dir.$filesInfo[$field][$key]["name"];
  70. $filesInfo[$field][$key]["file"] = $this->path.$filesInfo[$field][$key]["name"];
  71. $filesInfo[$field][$key]["size"] = $_FILES[$field]["size"][$key];
  72. $filesInfo[$field][$key]["error"] = $this->_getErrorCode(100, true);
  73. if((!$this->force && is_file($filesInfo[$field][$key]["file"]) && md5_file($filesInfo[$field][$key]["file"]) === $filesInfo[$field][$key]["md5"]) ||
  74. move_uploaded_file($_FILES[$field]["tmp_name"][$key], $filesInfo[$field][$key]["src"]))
  75. {
  76. $filesInfo[$field][$key]["error"] = 0;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. else{
  83. $filesInfo[$field][0]["name"] = $_FILES[$field]["name"];
  84. $filesInfo[$field][0]["error"] = $this->_getErrorCode($_FILES[$field]["error"], true);
  85. if($filesInfo[$field][0]["error"] == 0){
  86. $fileExt = File::getExt($_FILES[$field]["name"]);
  87. if($fileExt == null){
  88. $fileExt = File::getType($_FILES[$field]["tmp_name"]);
  89. }
  90. if($this->allowTypes !== "*"){
  91. if(!is_array($this->allowTypes)){
  92. $filesInfo[$field][0]["error"] = $this->_getErrorCode(1);
  93. }
  94. else if(is_array($fileExt) && !array_intersect($fileExt, $this->allowTypes)){
  95. $filesInfo[$field][0]["error"] = $this->_getErrorCode(1);
  96. }
  97. else if(!is_array($fileExt) && !in_array($fileExt, $this->allowTypes)){
  98. $filesInfo[$field][0]["error"] = $this->_getErrorCode(1);
  99. }
  100. }
  101. else if($_FILES[$field]["size"] > $this->maxSize){
  102. $filesInfo[$field][0]["error"] = $this->_getErrorCode(2);
  103. }
  104. if($filesInfo[$field][0]["error"] == 0 && is_uploaded_file($_FILES[$field]["tmp_name"])){
  105. if(is_array($fileExt))
  106. $fileExt = current($fileExt);
  107. $filesInfo[$field][0]["md5"] = md5_file($_FILES[$field]["tmp_name"]);
  108. $filesInfo[$field][0]["name"] = isset($this->fieldsMappings[$field][0]) ? $this->fieldsMappings[$field][0].".".$fileExt : $filesInfo[$field][0]["md5"].".".$fileExt;
  109. $filesInfo[$field][0]["ext"] = $fileExt;
  110. $filesInfo[$field][0]["path"] = $this->path;
  111. $filesInfo[$field][0]["dir"] = $this->dir;
  112. $filesInfo[$field][0]["src"] = $this->dir.$filesInfo[$field][0]["name"];
  113. $filesInfo[$field][0]["file"] = $this->path.$filesInfo[$field][0]["name"];
  114. $filesInfo[$field][0]["size"] = $_FILES[$field]["size"];
  115. $filesInfo[$field][0]["error"] = $this->_getErrorCode(100, true);
  116. if((!$this->force && is_file($filesInfo[$field][0]["file"]) && md5_file($filesInfo[$field][0]["file"]) === $filesInfo[$field][0]["md5"]) ||
  117. move_uploaded_file($_FILES[$field]["tmp_name"], $filesInfo[$field][0]["file"]))
  118. {
  119. $filesInfo[$field][0]["error"] = 0;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. return $filesInfo;
  126. }
  127. }