Skip to content

Instantly share code, notes, and snippets.

@GabrieliRoldao
Created December 12, 2016 11:08
Show Gist options
  • Select an option

  • Save GabrieliRoldao/cf8ccd1d8f8582933b05ac4078fd1d15 to your computer and use it in GitHub Desktop.

Select an option

Save GabrieliRoldao/cf8ccd1d8f8582933b05ac4078fd1d15 to your computer and use it in GitHub Desktop.
Métodos para verificação de arquivos image.
public function fazUpload(Request $request) {
if( $request->hasFile('file') ) {
$files = $request->file('file');
$verificacaoMime = $this->verificaMime($files);
if ( sizeof($verificacaoMime) > 0) {
return redirect()
->route('upload')
->with(['errors' => $verificacaoMime]);
}
foreach ( $files as $file ) {
if ( $file->isValid() ) {
$nameFile = $file->getClientOriginalName();
//$pathToSave = public_path('imgs');
$pathToSave = storage_path('app/public/imgs');
$file->move($pathToSave, $nameFile);
}
}
return redirect()->route('upload')->with(['success' => 'Arquivo(s) upados']);
}
return redirect()
->route('upload')
->with(['failed' => 'Não foi possível upar o(s) arquivo(s)']);
}
private function verificaMime (array $files)
{
$arquivosNaoImage = [];
foreach ( $files as $file ) {
if( $file->getMimeType() != 'image/png' && $file->getMimeType() != 'image/jpeg') {
$arquivosNaoImage[] = "O arquivo {$file->getClientOriginalName()} não é uma imagem";
}
}
return $arquivosNaoImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment