Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Uploader
- {
- protected $path;
- protected $data = [];
- protected $formName;
- protected $maxSize = 5242880;
- public function __construct($path, $formName)
- {
- $this->path = $path;
- $this->formName = $formName;
- $this->fileValidate();
- }
- public function isUploaded()
- {
- if(!empty($_FILES[$this->formName]['name'])){
- return true;
- } else {
- return false;
- }
- }
- protected function setFile()
- {
- if ($this->isUploaded()) {
- $data = [];
- for ($i = 0; $i < count($_FILES[$this->formName]['name']); $i++) {
- $data[] = new UploadFile(
- $_FILES[$this->formName]['name'][$i],
- $_FILES[$this->formName]['tmp_name'][$i],
- $_FILES[$this->formName]['error'][$i],
- $_FILES[$this->formName]['type'][$i],
- $_FILES[$this->formName]['size'][$i]
- );
- }
- return $data;
- }
- }
- protected function fileValidate()
- {
- $data = $this->setFile();
- $this->data = [];
- foreach ($data as $id => $item) {
- if ($this->maxSize >= $item->getSize() &&
- in_array($item->getType(), ['image/jpeg', 'image/png', 'image/gif'])) {
- if(getimagesize($item->getTmp())) {
- $this->data[] = $item;
- }
- }
- }
- }
- public function uploadFile()
- {
- foreach ($this->data as $file){
- if(0 == $file->getError()){
- move_uploaded_file($file->getTmp(), $this->path . $file->getFileName());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement