Created
December 12, 2016 11:08
-
-
Save GabrieliRoldao/cf8ccd1d8f8582933b05ac4078fd1d15 to your computer and use it in GitHub Desktop.
Métodos para verificação de arquivos image.
This file contains hidden or 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
| 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