Combu Server  3.1.1
PHP API Documentation
FileUpload.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Combu;
4 
8 class FileUpload {
9 
10  private $inputName = "";
11  private $createTempStruct = true;
12  private $rootPath = "";
13  private $destRootPath = "";
14  private $destFilePath = "";
15  private $allowedExtensions = array();
16 
24  public function __construct($inputName, $rootPath = null, $createTempStruct = true) {
25  $this->inputName = $inputName;
26  $this->createTempStruct = $createTempStruct;
27  if ($rootPath) {
28  $this->rootPath = $rootPath;
29  if (substr($this->rootPath, -1) != "/") {
30  $this->rootPath .= "/";
31  }
32  } else {
33  $this->rootPath = UPLOAD_FOLDER;
34  }
35  }
36 
42  public function SetAllowedExtensions($allowedExtensions) {
43  if (!is_array($allowedExtensions)) {
44  $allowedExtensions = array($allowedExtensions);
45  }
46  $this->allowedExtensions = array();
47  foreach ($allowedExtensions as $ext) {
48  if (substr($ext, 0, 1) != ".") {
49  $ext = '.' . $ext;
50  }
51  $this->allowedExtensions[] = $ext;
52  }
53  }
54 
60  public function IsUploaded() {
61  if (isset($_FILES[$this->inputName])) {
62  $file = $_FILES[$this->inputName];
63  // Is it uploaded?
64  if (is_uploaded_file($file['tmp_name']) && $file['size'] > 0) {
65  return TRUE;
66  }
67  }
68  return FALSE;
69  }
70 
71  function GetTempname($fileExt = ".tmp") {
72  $tmp_name = md5(microtime()) . $fileExt;
73  return $tmp_name;
74  }
75 
76  function GetFilenameExtension($filename) {
77  $i = strrpos($filename, ".");
78  if ($i === FALSE) {
79  return "";
80  }
81  return substr($filename, $i);
82  }
83 
89  public function Upload() {
90  $success = FALSE;
91  $this->destFilePath = "";
92  $this->destRootPath = "";
93  // Is it uploaded?
94  if ($this->IsUploaded()) {
95  $file = $_FILES[$this->inputName];
96  // Get the base file name
97  $basename = basename($file['name']);
98  // Make sure we have a file name
99  if (!$basename) {
100  $basename = basename($file['tmp_name']);
101  }
102  // Check if the file extension is allowed
103  if (!$this->IsAllowedExtension($basename)) {
104  return false;
105  }
106  // Create the destination folder structure
107  $path = $this->GetDestinationFolder();
108  if (!$path) {
109  return false;
110  }
111  $path = $path . $this->GetTempname($this->GetFilenameExtension($basename));
112  // Try to move the uploaded file to the destination folder
113  if (move_uploaded_file($file['tmp_name'], $path)) {
114  // Extract the destination url
115  $this->destRootPath = str_replace('\\', '/', substr($path, strlen($this->rootPath)));
116  // Set the destination full path
117  $this->destFilePath = $path;
118  $success = true;
119  }
120  }
121  return $success;
122  }
123 
130  private function IsAllowedExtension($fileName) {
131  if (count($this->allowedExtensions) == 0) {
132  return true;
133  }
134  $fileExt = $this->GetFilenameExtension($fileName);
135  foreach ($this->allowedExtensions as $ext) {
136  if (strcasecmp($ext, $fileExt) == 0) {
137  return true;
138  }
139  }
140  return false;
141  }
142 
148  private function GetDestinationFolder() {
149  if (!file_exists($this->rootPath)) {
150  return false;
151  }
152  $folder = $this->rootPath . date("Y");
153  if (!file_exists($folder)) {
154  @mkdir($folder);
155  }
156  $folder .= '/' . date("m");
157  if (!file_exists($folder)) {
158  @mkdir($folder);
159  }
160  $folder .= '/' . date("d");
161  if (!file_exists($folder)) {
162  @mkdir($folder);
163  }
164  if (!file_exists($folder)) {
165  return false;
166  }
167  return $folder . "/";
168  }
169 
175  public function GetDestinationPath() {
176  return $this->destFilePath;
177  }
178 
184  public function GetDestinationUrl() {
185  return $this->destRootPath;
186  }
187 
188 }
SetAllowedExtensions($allowedExtensions)
Definition: FileUpload.php:42
__construct($inputName, $rootPath=null, $createTempStruct=true)
Definition: FileUpload.php:24
GetTempname($fileExt=".tmp")
Definition: FileUpload.php:71
Definition: Account.php:3
GetFilenameExtension($filename)
Definition: FileUpload.php:76