Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. class Uploader
  2. {
  3. protected $path;
  4. protected $data = [];
  5. protected $formName;
  6. protected $maxSize = 5242880;
  7.  
  8. public function __construct($path, $formName)
  9. {
  10. $this->path = $path;
  11. $this->formName = $formName;
  12. $this->fileValidate();
  13. }
  14.  
  15. public function isUploaded()
  16. {
  17. if(!empty($_FILES[$this->formName]['name'])){
  18. return true;
  19. } else {
  20. return false;
  21. }
  22. }
  23.  
  24. protected function setFile()
  25. {
  26. if ($this->isUploaded()) {
  27. $data = [];
  28. for ($i = 0; $i < count($_FILES[$this->formName]['name']); $i++) {
  29. $data[] = new UploadFile(
  30. $_FILES[$this->formName]['name'][$i],
  31. $_FILES[$this->formName]['tmp_name'][$i],
  32. $_FILES[$this->formName]['error'][$i],
  33. $_FILES[$this->formName]['type'][$i],
  34. $_FILES[$this->formName]['size'][$i]
  35. );
  36. }
  37. return $data;
  38. }
  39. }
  40.  
  41. protected function fileValidate()
  42. {
  43. $data = $this->setFile();
  44. $this->data = [];
  45.  
  46. foreach ($data as $id => $item) {
  47. if ($this->maxSize >= $item->getSize() &&
  48. in_array($item->getType(), ['image/jpeg', 'image/png', 'image/gif'])) {
  49. if(getimagesize($item->getTmp())) {
  50. $this->data[] = $item;
  51. }
  52. }
  53. }
  54. }
  55.  
  56. public function uploadFile()
  57. {
  58. foreach ($this->data as $file){
  59. if(0 == $file->getError()){
  60. move_uploaded_file($file->getTmp(), $this->path . $file->getFileName());
  61. }
  62. }
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement