【发布时间】:2015-04-16 06:15:50
【问题描述】:
我在控制器中有这个动作
public function upload() {
// getting all of the post data
$file = array('image' => Input::file('image'));
//return $file;
// setting up rules
$rules = array('image' => 'required'); //mimes:jpeg,bmp,png and for max size max:10000
// doing the validation, passing post data, rules and the messages
$validator = Validator::make($file, $rules);
if ($validator->fails()) {
// send back to the page with the input data and errors
// return Redirect::to('upload')->withInput()->withErrors($validator);
return "error validation";
}
else {
// checking file is valid.
if (Input::file('image')->isValid()) {
$destinationPath = 'myuploads'; // upload path
$extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
$fileName = Input::file('image')->getClientOriginalName();
// $fileName = rand(11111,99999).'.'.$extension; // renameing image
Input::file('image')->move($destinationPath, $fileName); // uploading file to given path
return "upload success";
}
else {
// sending back with error message.
Session::flash('error', 'uploaded file is not valid');
return "error";
}
}
}
它适用于像 2MB 这样的小文件,但不适用于 4MB 的文件。 对于 4MB 或更多,它会在验证中出错。在上面的代码中有这个代码
if ($validator->fails()) {
return "error validation";
}
它给出了这个自定义错误error validation。我已经为最大上传限制和发布限制配置了 php.ini。
【问题讨论】:
-
我知道您在
php.ini中设置了upload_max_filesize和post_max_size,但您是否检查过这些设置是否已应用?我经常发现 php.ini 中的设置在其他地方被覆盖,ini_get('post_max_size')返回什么? -
您是否检查了
max_execution_time并增加了默认值? Related -
查看
$validator->messages();会得到什么? -
@Chelsea 您是否在模型中设置了验证规则。请参考此链接laravel validation 并设置文件大小验证,例如 $rules = array('file' => 'required|max:10000|mimes:doc,docx')
-
小文件的扩展名是什么?在大文件中?可能是扩展大文件错误。