10 private $inputName =
"";
11 private $createTempStruct =
true;
12 private $rootPath =
"";
13 private $destRootPath =
"";
14 private $destFilePath =
"";
15 private $allowedExtensions = array();
24 public function __construct($inputName, $rootPath = null, $createTempStruct =
true) {
25 $this->inputName = $inputName;
26 $this->createTempStruct = $createTempStruct;
28 $this->rootPath = $rootPath;
29 if (substr($this->rootPath, -1) !=
"/") {
30 $this->rootPath .=
"/";
33 $this->rootPath = UPLOAD_FOLDER;
43 if (!is_array($allowedExtensions)) {
44 $allowedExtensions = array($allowedExtensions);
46 $this->allowedExtensions = array();
47 foreach ($allowedExtensions as $ext) {
48 if (substr($ext, 0, 1) !=
".") {
51 $this->allowedExtensions[] = $ext;
61 if (isset($_FILES[$this->inputName])) {
62 $file = $_FILES[$this->inputName];
64 if (is_uploaded_file($file[
'tmp_name']) && $file[
'size'] > 0) {
72 $tmp_name = md5(microtime()) . $fileExt;
77 $i = strrpos($filename,
".");
81 return substr($filename, $i);
91 $this->destFilePath =
"";
92 $this->destRootPath =
"";
94 if ($this->IsUploaded()) {
95 $file = $_FILES[$this->inputName];
97 $basename = basename($file[
'name']);
100 $basename = basename($file[
'tmp_name']);
103 if (!$this->IsAllowedExtension($basename)) {
107 $path = $this->GetDestinationFolder();
111 $path = $path . $this->GetTempname($this->GetFilenameExtension($basename));
113 if (move_uploaded_file($file[
'tmp_name'], $path)) {
115 $this->destRootPath = str_replace(
'\\',
'/', substr($path, strlen($this->rootPath)));
117 $this->destFilePath = $path;
130 private function IsAllowedExtension($fileName) {
131 if (count($this->allowedExtensions) == 0) {
134 $fileExt = $this->GetFilenameExtension($fileName);
135 foreach ($this->allowedExtensions as $ext) {
136 if (strcasecmp($ext, $fileExt) == 0) {
148 private function GetDestinationFolder() {
149 if (!file_exists($this->rootPath)) {
152 $folder = $this->rootPath . date(
"Y");
153 if (!file_exists($folder)) {
156 $folder .=
'/' . date(
"m");
157 if (!file_exists($folder)) {
160 $folder .=
'/' . date(
"d");
161 if (!file_exists($folder)) {
164 if (!file_exists($folder)) {
167 return $folder .
"/";
176 return $this->destFilePath;
185 return $this->destRootPath;
SetAllowedExtensions($allowedExtensions)
__construct($inputName, $rootPath=null, $createTempStruct=true)
GetTempname($fileExt=".tmp")
GetFilenameExtension($filename)