OpenSencillo  2016.106
Long live the simplicity of PHP
 All Data Structures Namespaces Files Functions Pages
files.upload.fup.php
1 <?php
11 class upload
12 {
13  public $mime;
14  protected $uploadDirectory;
15  protected $size;
16  protected $uploadInfo;
17  protected $mode;
18  protected $status;
19  public $ajax = false;
20 
21  public function __construct($path)
22  {
23  $this->mime=null;
24  $this->path($path);
25  $this->advancedMode();
26  }
27 
34  protected function mimeTest($mime)
35  {
36  if(($mime == $_FILES['FileInput']['type'])&&(isset($mime)&&(!empty($mime))))
37  {
38  $this->mime=true;
39  }
40  }
41 
48  public function mimeConfig($mime=null)
49  {
50  if(is_array($mime))
51  {
52  foreach($mime as $val)
53  {
54  $this->mimeTest($val);
55  }
56  }
57  else
58  {
59  $this->mimeTest($mime);
60  }
61 
65  if($mime===null)
66  {
67  $this->mime=true;
68  }
69  }
70 
76  public function setMimes($mime=null)
77  {
78  if(is_array($mime))
79  {
80  $this->mime=$mime;
81  }
82  else
83  {
84  die(__CLASS__.':'.__METHOD__.':line='.__LINE__);
85  }
86  }
87 
93  public function path($path)
94  {
95  $this->uploadDirectory = $path;//example '/home/website/file_upload/uploads/'
96  }
97 
103  public function maxSize($size)
104  {
105  $this->size = $size;
106  }
107 
113  public function advancedMode($mode=null)
114  {
115  switch($mode)
116  {
117  case true:
118  $this->mode = true;
119  break;
120  default:
121  $this->mode = false;
122  break;
123  }
124  }
125 
129  final public function ajaxSendJson($respond=null)
130  {
131  if($respond==null)
132  {
133  return json_encode($this->status);
134  }
135  else
136  {
137  return json_encode($this->status[$respond]);
138  }
139  }
140 
144  public function name()
145  {
146  return $this->uploadInfo['newName'];
147  }
148 
160  public function upload()
161  {
162  $this->status = array();
163  if(isset($_FILES["FileInput"])/* && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK*/)
164  {
165  $UploadDirectory = $this->uploadDirectory; //specify upload directory ends with / (slash)
166 
167  /*
168  Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini".
169  Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit
170  and set them adequately, also check "post_max_size".
171  */
172 
173  //check if this is an ajax request
174  if($this->mode)
175  {
176  $this->status['mode']='advanced';
177  if($this->ajax===true)
178  {
179  $this->status['ajax']=true;
180  if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']))
181  {
182  $this->status['code']="444";
183  return $this->status;
184  }
185  }
186  else
187  {
188  $this->status['ajax']=false;
189  }
190 
191  //Is file size is less than allowed size.
192  if ($_FILES["FileInput"]["size"] > $this->size)
193  {
194  $this->status['code']="413";
195  return $this->status;
196  }
197 
198  //allowed file type Server side check
199  if(in_array(strtolower($_FILES['FileInput']['type']),$this->mime)===true)
200  {
201  $this->status['mime']=$_FILES['FileInput']['type'];
202  }
203  else
204  {
205  $this->status['code']="415-0"; //output error
206  return $this->status;
207  }
208  }
209  else
210  {
211  $this->status['mode']='simple';
212  }
213 
214  $File_Name = strtolower($_FILES['FileInput']['name']);
215  $File_Ext = substr($File_Name, strrpos($File_Name, '.')); //get file extention
216  $Random_Number = rand(0, 9999999999).date('YmdHis'); //Random number to be added to name.
217  $NewFileName = $Random_Number.$File_Ext; //new file name
218 
219  $this->uploadInfo = array('oldName'=>$File_Name,
220  'ext'=>$File_Ext,
221  'rnd'=>$Random_Number,
222  'newName'=>$NewFileName);
223  $this->status['info'] = $this->uploadInfo;
224 
225  if(!is_dir($UploadDirectory))
226  {
227  mkdir($UploadDirectory,0777);
228  }
229  if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName))
230  {
231  // do other stuff
232  $this->status['code']="200";
233  $this->status['name']=$NewFileName;
234  $this->status['path']=$UploadDirectory;
235  }
236  else
237  {
238  $this->status['code']="417";
239  }
240 
241  }
242  else
243  {
244  $this->status['code']="404";
245  }
246 
247  return $this->status;
248  }
249 }
250 ?>