Skip to content

Instantly share code, notes, and snippets.

@thephucit
Created December 20, 2020 02:51
Show Gist options
  • Save thephucit/ad7bd57e08da161dbf46af1777dad16b to your computer and use it in GitHub Desktop.
Save thephucit/ad7bd57e08da161dbf46af1777dad16b to your computer and use it in GitHub Desktop.
Laravel validate file upload
<?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