有两种情况:文件大小大于php变量upload_max_filesize,第二种情况是大于post_max_size变量.在第一个例子中引发了一个例外,因此它很容易捕获它.在第二种情况下没有例外,我使用this问题来解决它.
现在,检查此代码:在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)));
}
}