Created
June 26, 2024 14:11
-
-
Save NandoKstroNet/0336e0f60733d4afa8970ce26aa93ebf to your computer and use it in GitHub Desktop.
Conteúdos apoio do curso API REST com PHP na https://codeexperts.com.br
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Coloque na classe service... | |
protected function normalizarUploadData($arquivos): array | |
{ | |
$arquivosNormalizados = []; | |
for ($i = 0; $i < count($arquivos['name']); $i++) { | |
$arquivosNormalizados[$i]['name'] = $arquivos['name'][$i]; | |
$arquivosNormalizados[$i]["full_path"] = $arquivos['full_path'][$i]; | |
$arquivosNormalizados[$i]["type"] = $arquivos['type'][$i]; | |
$arquivosNormalizados[$i]["tmp_name"] = $arquivos['tmp_name'][$i]; | |
$arquivosNormalizados[$i]["error"] = $arquivos['error'][$i]; | |
} | |
return $arquivosNormalizados; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Coloque na classe service... | |
public function upload($arquivo, $subpasta = null, $imagemRemover = null) | |
{ | |
$extensao = strrchr($arquivo['name'], '.'); | |
$novoNome = md5($arquivo['name']) . uniqid() . $extensao; | |
if (!is_dir(PASTA_UPLOADS)) { | |
mkdir(PASTA_UPLOADS, 755, true); | |
} | |
if (!is_dir(PASTA_UPLOADS . $subpasta)) { | |
mkdir(PASTA_UPLOADS . $subpasta, 755, true); | |
} | |
$novoNome = $subpasta ? $subpasta . '/' . $novoNome | |
: $novoNome; | |
if (!move_uploaded_file($arquivo['tmp_name'], PASTA_UPLOADS . $novoNome)) { | |
return false; | |
} | |
if ($imagemRemover && file_exists(PASTA_UPLOADS . $imagemRemover)) { | |
unlink(PASTA_UPLOADS . $imagemRemover); | |
} | |
return $novoNome; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment