Php 处理Laravel 4上载大文件异常
我的目标是管理max upload file异常,并显示客户端友好的消息,但我不知道控制该异常的最佳位置。这是我的控制器方法:Php 处理Laravel 4上载大文件异常,php,file-upload,extjs4,laravel-4,Php,File Upload,Extjs4,Laravel 4,我的目标是管理max upload file异常,并显示客户端友好的消息,但我不知道控制该异常的最佳位置。这是我的控制器方法: public function upload_file() { if (!Input::hasFile('file')) return; $utils = App::make('utils'); $file = Input::file('file'); $name = I
public function upload_file()
{
if (!Input::hasFile('file'))
return;
$utils = App::make('utils');
$file = Input::file('file');
$name = Input::get('name');
$size = $file->getSize();
if ($size > FileModel::$max_file_size)
return json_encode(array('success'=>false, 'message'=>sprintf('The file size should be lower than %smb.',FileModel::$max_file_size/1000000)));
$original_file_name = $file->getClientOriginalName();
$destination_directory = "";
$final_file_name = $utils->copy_file_to_location($file);
return json_encode(array('success'=>true, 'file'=>$original_file_name));
}
这是utils复制文件到位置的方法:
public function copy_file_to_location($file, $destination_directory = "")
{
if (!isset($file))
return;
$file_name = time()."_".$file->getClientOriginalName();
$file->move(app_path().'/storage/files/'.$destination_directory, $file_name);
return $file_name;
}
我不知道在哪里处理上传文件时引发的异常,这些文件的大小比服务器max upload file size变量大。我应该在何处以及如何处理此问题以显示用户友好的消息,并且不锁定用户界面。顺便说一下,我正在客户端使用ExtJS4。谢谢
编辑
我发现一个相关的问题很有帮助(这是同一个问题),但我需要知道,在Laravel内部,我应该在哪里检查这个问题。您在相关问题中看到的“上传最大文件大小”和“发布最大文件大小”设置不是由Laravel处理的,它是php安装中的php.ini配置文件的一部分。有两种情况:文件大小大于php变量
upload\u max\u filesize
,第二种情况是大于post\u max\u size
变量。在第一种方法中,会引发一个异常,以便于它捕获它。在第二种情况下,并没有例外,我用这个问题来解决它
现在,在哪里检查此代码:在Laravel控制器aciton方法中。我认为控制器动作中的代码从未被删减,但我错了。最后,这是解决这个问题的方法:
public function upload_file()
{
$file_max = ini_get('upload_max_filesize');
$file_max_str_leng = strlen($file_max);
$file_max_meassure_unit = substr($file_max,$file_max_str_leng - 1,1);
$file_max_meassure_unit = $file_max_meassure_unit == 'K' ? 'kb' : ($file_max_meassure_unit == 'M' ? 'mb' : ($file_max_meassure_unit == 'G' ? 'gb' : 'unidades'));
$file_max = substr($file_max,0,$file_max_str_leng - 1);
$file_max = intval($file_max);
//handle second case
if((empty($_FILES) && empty($_POST) && isset($_SERVER['REQUEST_METHOD']) && strtolower($_SERVER['REQUEST_METHOD']) == 'post'))
{ //catch file overload error...
//grab the size limits...
return json_encode(array('success'=>false, 'message'=>sprintf('The file size should be lower than %s%s.',$file_max,$file_max_meassure_unit)));
}
try{
if (!Input::hasFile('file'))
return;
$utils = App::make('utils');
$file = Input::file('file');
$name = Input::get('name');
$size = $file->getSize();
if ($size > $file_max)
return json_encode(array('success'=>false, 'message'=>sprintf('El tamaño del archivo debe ser menor que %smb.',$file_max)));
$original_file_name = $file->getClientOriginalName();
$destination_directory = "";
$final_file_name = $utils->copy_file_to_location($file);
return json_encode(array('success'=>true, 'file'=>$original_file_name));
}
catch (Exception $e)
{
//handle first case
return json_encode(array('success'=>false, 'message'=>sprintf('The file size should be lower than %s%s.',$file_max,$file_max_meassure_unit)));
}
}
旧帖子,但仍然是一个相关的问题。 使用\Lightning\Foundation\Http\Middleware\VerifyPostSize中间件 并在Handler.php render()方法中处理错误 检查中间件文档 这是我在Handler.php中的方法:
public function render($request, \Exception $exception)
{
//thats the json your client will recieve as a response, can be anything you need
$response = [
'code' => 500,
'status' => 'error',
'message' => 'Internal server error.',
'exception' => get_class($exception),
'exception_message' => $exception->getMessage(),
'url' => $request->decodedPath(),
'method' => $request->method(),
];
if (env('APP_ENV') === 'local') {
$response['trace'] = $exception->getTrace();
}
if ($exception instanceof PostTooLargeException) {
$response['code'] = 413; //Payload too large
$response['message'] = $response['message'] .' The maximum request size is: ' .ini_get('post_max_size');
}
return response()->json($response, $response['code']);
}