Open Sencillo  2015.003
Long live the simplicity of PHP
 All Data Structures Namespaces Functions
files.upload.fup.php
1 <?php
11 class upload
12 {
13  protected $mime;
14  protected $uploadDirectory;
15  protected $size;
16  protected $uploadInfo;
17  protected $mode;
18  protected $status;
19 
20  public function __construct($path)
21  {
22  $this->mime=null;
23  $this->path($path);
24  $this->advancedMode();
25  }
26 
32  protected function mimeTest($mime)
33  {
34  if($val == $_FILES['FileInput']['type'])
35  {
36  $this->mime=true;
37  }
38  }
39 
45  public function mimeConfig($mime=null)
46  {
47  if(is_array($mime))
48  {
49  foreach($mime as $val)
50  {
51  $this->mimeTest($val);
52  }
53  }
54  else
55  {
56  $this->mimeTest($mime);
57  }
58 
62  if($mime==null)
63  {
64  $this->mime=true;
65  }
66  }
67 
73  public function path($path)
74  {
75  $this->uploadDirectory = $path;//example '/home/website/file_upload/uploads/'
76  }
77 
83  public function maxSize($size)
84  {
85  $this->size = $size;
86  }
87 
93  public function advancedMode($mode=null)
94  {
95  switch($mode)
96  {
97  case true:
98  $this->mode = true;
99  break;
100  default:
101  $this->mode = false;
102  break;
103  }
104  }
105 
109  final public function ajaxSendJson($respond=null)
110  {
111  if($respond==null)
112  {
113  print json_encode($this->status);
114  }
115  else
116  {
117  print json_encode($this->status[$respond]);
118  }
119  }
120 
124  public function name()
125  {
126  return $this->uploadInfo['newName'];
127  }
128 
140  public function upload()
141  {
142  $this->status = array();
143  if(isset($_FILES["FileInput"])/* && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK*/)
144  {
145  $UploadDirectory = $this->uploadDirectory; //specify upload directory ends with / (slash)
146 
147  /*
148  Note : You will run into errors or blank page if "memory_limit" or "upload_max_filesize" is set to low in "php.ini".
149  Open "php.ini" file, and search for "memory_limit" or "upload_max_filesize" limit
150  and set them adequately, also check "post_max_size".
151  */
152 
153  //check if this is an ajax request
154  if($this->mode)
155  {
156  $this->status['mode']='advanced';
157  if (!isset($_SERVER['HTTP_X_REQUESTED_WITH']))
158  {
159  $this->status['code']="444";
160  }
161 
162  //Is file size is less than allowed size.
163  if ($_FILES["FileInput"]["size"] > $this->size)
164  {
165  $this->status['code']="413";
166  }
167 
168  //allowed file type Server side check
169  if($this->mime==true)
170  {
171  switch(strtolower($_FILES['FileInput']['type']))
172  {
173  //allowed file types
174  case 'image/png':
175  case 'image/gif':
176  case 'image/jpeg':
177  case 'image/pjpeg':
178  case 'text/plain':
179  case 'text/html': //html file
180  case 'application/x-zip-compressed':
181  case 'application/pdf':
182  case 'application/msword':
183  case 'application/vnd.ms-excel':
184  case 'video/mp4':
185  $this->status['mime']=$_FILES['FileInput']['type'];
186  break;
187  default:
188  $this->status['code']="415-0"; //output error
189  }
190  }
191  else
192  {
193  $this->status['code']="415-1";//output error
194  }
195  }
196  else
197  {
198  $this->status['mode']='simple';
199  }
200 
201  $File_Name = strtolower($_FILES['FileInput']['name']);
202  $File_Ext = substr($File_Name, strrpos($File_Name, '.')); //get file extention
203  $Random_Number = rand(0, 9999999999).date('YmdHis'); //Random number to be added to name.
204  $NewFileName = $Random_Number.$File_Ext; //new file name
205 
206  $this->uploadInfo = array('oldName'=>$File_Name,
207  'ext'=>$File_Ext,
208  'rnd'=>$Random_Number,
209  'newName'=>$NewFileName);
210  $this->status['info'] = $this->uploadInfo;
211 
212  if(!is_dir($UploadDirectory))
213  {
214  mkdir($UploadDirectory,0777);
215  }
216  if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName))
217  {
218  // do other stuff
219  $this->status['code']="200";
220  $this->status['name']=$NewFileName;
221  }
222  else
223  {
224  $this->status['code']="417";
225  }
226 
227  }
228  else
229  {
230  $this->status['code']="404";
231  }
232 
233  if($this->status['code']=='200')
234  {
235  $this->ajaxSendJson('name');
236  }
237  else
238  {
239  $this->ajaxSendJson('code');
240  }
241  }
242 }
243 ?>
maxSize($size)
ajaxSendJson($respond=null)
path($path)
mimeConfig($mime=null)
advancedMode($mode=null)
mimeTest($mime)