通过中间件检测 Laravel 上的图片上传
Detect Image upload on Laravel through middleware
我希望能够在 Laravel
上的全局 middleware
中检测图像。
我的目标是让 middleware
检测当前是否有正在上传的图像,然后检测其大小并对其进行操作。
这可能吗?
我继承了一个项目,需要操作每个全局图像upload
,但是有太多单独的脚本无法单独更改它们,我需要某种方法来全局更改它们.
我乐于接受建议!
<?php
namespace App\Http\Middleware;
use Closure;
use Symfony\Component\HttpFoundation\Response;
class ImageInterceptorMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
foreach (array_flatten($request->files->all()) as $file) {
$size = $file->getClientSize(); // size in bytes!
$onemb = pow(1024, 2); //
if ($size > $onemb) {
abort(Response::HTTP_UNPROCESSABLE_ENTITY);
}
}
return $next($request);
}
}
我希望能够在 Laravel
上的全局 middleware
中检测图像。
我的目标是让 middleware
检测当前是否有正在上传的图像,然后检测其大小并对其进行操作。
这可能吗?
我继承了一个项目,需要操作每个全局图像upload
,但是有太多单独的脚本无法单独更改它们,我需要某种方法来全局更改它们.
我乐于接受建议!
<?php
namespace App\Http\Middleware;
use Closure;
use Symfony\Component\HttpFoundation\Response;
class ImageInterceptorMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
foreach (array_flatten($request->files->all()) as $file) {
$size = $file->getClientSize(); // size in bytes!
$onemb = pow(1024, 2); //
if ($size > $onemb) {
abort(Response::HTTP_UNPROCESSABLE_ENTITY);
}
}
return $next($request);
}
}