123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- /**
- * The Feeler framework, licensed under LGPL license
- * Author: Rick Kuo
- */
- class Uploader{
- public $dir;
- public $path;
- public $maxSize;
- public $force;
- public $allowTypes = "*";
- public $checkTypes = array();
- public $fieldsMappings = array();
-
- private $_sysErrCodeBaseNum = 1000;
- private $_commonErrCodeBaseNum = 2000;
-
- function __construct($dir, $maxSize = 8096, $force = false){
- $dir .= date("Y/m/d/");
- $this->dir = $dir;
- $this->path = Yii::app()->basePath."/../".$dir;
- $this->maxSize = $maxSize * 1024;
- $this->force = $force;
- }
-
- private function _getErrorCode($errCode, $sys = false){
- if(is_numeric($errCode) && $errCode){
- if($sys) $errCode += $this->_sysErrCodeBaseNum;
- else $errCode += $this->_commonErrCodeBaseNum;
- }
-
- return $errCode;
- }
-
- public function act(){
- $filesInfo = array();
- File::mkdir($this->path);
-
- foreach($_FILES as $field => $file){
- $fileInfo = array();
-
- if(!isset($_FILES[$field]["name"]) || $_FILES[$field]["name"] === "")
- continue;
-
- if(is_array($_FILES[$field]["name"])){
- $keys = array_keys($_FILES[$field]["name"]);
-
- foreach($keys as $key){
- $filesInfo[$field][$key]["name"] = $_FILES[$field]["name"][$key];
- $filesInfo[$field][$key]["error"] = $this->_getErrorCode($_FILES[$field]["error"][$key], true);
-
- if($filesInfo[$field][$key]["error"] == 0){
- $fileExt = File::getExt($_FILES[$field]["name"][$key]);
- if($fileExt == null){
- $fileExt = File::getType($_FILES[$field]["tmp_name"][$key]);
- }
-
- if($this->allowTypes !== "*"){
- if(!is_array($this->allowTypes)){
- $filesInfo[$field][$key]["error"] = $this->_getErrorCode(1);
- }
- else if(is_array($fileExt) && !array_intersect($fileExt, $this->allowTypes)){
- $filesInfo[$field][$key]["error"] = $this->_getErrorCode(1);
- }
- else if(!is_array($fileExt) && !in_array($fileExt, $this->allowTypes)){
- $filesInfo[$field][$key]["error"] = $this->_getErrorCode(1);
- }
- }
- else if($_FILES[$field]["size"][$key] > $this->maxSize){
- $filesInfo[$field][$key]["error"] = $this->_getErrorCode(2);
- }
- if($filesInfo[$field][$key]["error"] == 0 && is_uploaded_file($_FILES[$field]["tmp_name"][$key])){
- if(is_array($fileExt))
- $fileExt = current($fileExt);
-
- $filesInfo[$field][$key]["md5"] = md5_file($_FILES[$field]["tmp_name"][$key]);
- $filesInfo[$field][$key]["name"] = isset($this->fieldsMappings[$field][$key]) ? $this->fieldsMappings[$field][$key].".".$fileExt : $filesInfo[$field][$key]["md5"].".".$fileExt;
- $filesInfo[$field][$key]["ext"] = $fileExt;
- $filesInfo[$field][$key]["path"] = $this->path;
- $filesInfo[$field][$key]["dir"] = $this->dir;
- $filesInfo[$field][$key]["src"] = $this->dir.$filesInfo[$field][$key]["name"];
- $filesInfo[$field][$key]["file"] = $this->path.$filesInfo[$field][$key]["name"];
- $filesInfo[$field][$key]["size"] = $_FILES[$field]["size"][$key];
-
- $filesInfo[$field][$key]["error"] = $this->_getErrorCode(100, true);
-
- if((!$this->force && is_file($filesInfo[$field][$key]["file"]) && md5_file($filesInfo[$field][$key]["file"]) === $filesInfo[$field][$key]["md5"]) ||
- move_uploaded_file($_FILES[$field]["tmp_name"][$key], $filesInfo[$field][$key]["src"]))
- {
- $filesInfo[$field][$key]["error"] = 0;
- }
- }
- }
- }
- }
- else{
- $filesInfo[$field][0]["name"] = $_FILES[$field]["name"];
- $filesInfo[$field][0]["error"] = $this->_getErrorCode($_FILES[$field]["error"], true);
-
- if($filesInfo[$field][0]["error"] == 0){
- $fileExt = File::getExt($_FILES[$field]["name"]);
- if($fileExt == null){
- $fileExt = File::getType($_FILES[$field]["tmp_name"]);
- }
-
- if($this->allowTypes !== "*"){
- if(!is_array($this->allowTypes)){
- $filesInfo[$field][0]["error"] = $this->_getErrorCode(1);
- }
- else if(is_array($fileExt) && !array_intersect($fileExt, $this->allowTypes)){
- $filesInfo[$field][0]["error"] = $this->_getErrorCode(1);
- }
- else if(!is_array($fileExt) && !in_array($fileExt, $this->allowTypes)){
- $filesInfo[$field][0]["error"] = $this->_getErrorCode(1);
- }
- }
- else if($_FILES[$field]["size"] > $this->maxSize){
- $filesInfo[$field][0]["error"] = $this->_getErrorCode(2);
- }
-
- if($filesInfo[$field][0]["error"] == 0 && is_uploaded_file($_FILES[$field]["tmp_name"])){
- if(is_array($fileExt))
- $fileExt = current($fileExt);
-
- $filesInfo[$field][0]["md5"] = md5_file($_FILES[$field]["tmp_name"]);
- $filesInfo[$field][0]["name"] = isset($this->fieldsMappings[$field][0]) ? $this->fieldsMappings[$field][0].".".$fileExt : $filesInfo[$field][0]["md5"].".".$fileExt;
- $filesInfo[$field][0]["ext"] = $fileExt;
- $filesInfo[$field][0]["path"] = $this->path;
- $filesInfo[$field][0]["dir"] = $this->dir;
- $filesInfo[$field][0]["src"] = $this->dir.$filesInfo[$field][0]["name"];
- $filesInfo[$field][0]["file"] = $this->path.$filesInfo[$field][0]["name"];
- $filesInfo[$field][0]["size"] = $_FILES[$field]["size"];
-
- $filesInfo[$field][0]["error"] = $this->_getErrorCode(100, true);
-
- if((!$this->force && is_file($filesInfo[$field][0]["file"]) && md5_file($filesInfo[$field][0]["file"]) === $filesInfo[$field][0]["md5"]) ||
- move_uploaded_file($_FILES[$field]["tmp_name"], $filesInfo[$field][0]["file"]))
- {
- $filesInfo[$field][0]["error"] = 0;
- }
- }
- }
- }
- }
-
- return $filesInfo;
- }
- }
|