Skip to content

Instantly share code, notes, and snippets.

@danieldsf
Created January 20, 2018 09:10
Show Gist options
  • Save danieldsf/1ad580bd76a8ea5349420a253913d768 to your computer and use it in GitHub Desktop.
Save danieldsf/1ad580bd76a8ea5349420a253913d768 to your computer and use it in GitHub Desktop.
<?php
App::uses('Component', 'Controller');
App::uses('Folder', 'Utility');
class UploadComponent extends Component{
const MAX_FILES = 4;
const DIR = WWW_ROOT.'upload'.DS;
public $endereco_atual = null;
public function upload($data = null){
if(!empty($data)){
#if(count($data) > UploadComponent::MAX_FILES){
# #@Todo: melhorar excecao;
# throw new NotFoundException("O numero maximo de uploads eh: {$this->max_files}", 1);
#}
#IF CONTINUE:
#foreach ($data as $file) {
$file = $data;
$error = $file['error'];
$size = $file['size'];
$filename = $file['name'];
$file_tmp_name = $file['tmp_name'];
$allowed = array('pdf', 'doc', 'docx');
if(!in_array(substr( strrchr($filename, '.'), 1), $allowed)){
throw new NotFoundException("Format File Denied", 1);
}elseif($error || $size == 0){
throw new NotFoundException("Sending Error", 1);
}else{
if(!is_dir(UploadComponent::DIR)){
$pasta = new Folder();
$pasta->create(UploadComponent::DIR);
}
if(is_uploaded_file($file_tmp_name)) {
$this->endereco_atual = UploadComponent::DIR.String::uuid().'-'.$filename;
move_uploaded_file($file_tmp_name, $this->endereco_atual);
}
}
#
#}
return $this->endereco_atual;
}else{
return false;
}
}
#End of class;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment