<?php
namespace App\Helpers;
class uploadImages {
protected $errors = [];
protected $image_name;
protected $success = false;
protected $maxsize = 16777216;
protected $acceptable = array('image/jpeg', 'image/jpg');
/**
* Upload image
*
* @param $data $_FILES information (name, type, temp_name, size, error, etc)
* @param $uploadLocation the server folder path where the image will be uploaded to
* @param $nameTag The nametag is the prefix to the uniqid function / the 'true' parameter is the more_entropy, read php uniqid for further information
*
* @return returns true if file uploaded successfully to location, false otherwise
*/
public function listen($data, $uploadLocation, $nameTag){
if($data['size'] != 0) {
$image_extension = pathinfo($data['name'], PATHINFO_EXTENSION);
$this->image_name = uniqid($nameTag, true) . '.' . $image_extension;
if($data['size'] >= $this->maxsize) {
$this->errors[] = 'File too large. File must be less than 16 megabytes.';
}
if(!in_array($data['type'], $this->acceptable)) {
$this->errors[] = 'Invalid file type. Only JPG types are accepted.';
}
if(count($this->errors) === 0) {
$this->success = move_uploaded_file($data['tmp_name'], $uploadLocation . '/' . $this->image_name);
}
} else {
$this->errors[] = 'You need to upload an image.';
}
return $this;
}
public function getFileName() {
return $this->image_name;
}
public function isUploaded() {
return $this->success;
}
public function getErrors() {
return $this->errors;
}
public function hasErrors(){
return (!empty($this->errors));
}
}