danieldsf
1/20/2018 - 9:10 AM

UploadComponent.php

<?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;
	}	
?>