Created
December 20, 2020 02:51
-
-
Save thephucit/ad7bd57e08da161dbf46af1777dad16b to your computer and use it in GitHub Desktop.
Laravel validate file upload
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 | |
use Illuminate\Http\UploadedFile; | |
use Validator; | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
Validator::extend('file_extension', function ($attribute, $value, $parameters, $validator) { | |
if (!$value instanceof UploadedFile) { | |
return false; | |
} | |
$extensions = implode(',', $parameters); | |
$validator->addReplacer('file_extension', function ( | |
$message, | |
$attribute, | |
$rule, | |
$parameters | |
) use ($extensions) { | |
return \str_replace(':values', $extensions, $message); | |
}); | |
$extension = strtolower($value->getClientOriginalExtension()); | |
return $extension !== '' && in_array($extension, $parameters); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment