在Laravel中上传小文件但不上传大文件

Che*_*ea 20 php laravel

我在控制器中有这个动作

    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";
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

它适用于2MB的小文件大小,但不适用于4MB文件大小.对于4MB或更多,它会在验证中出错.在上面的代码中有这个代码

 if ($validator->fails()) {

       return "error validation";
      }
Run Code Online (Sandbox Code Playgroud)

它给出了这个自定义错误error validation.我已经开始配置php.ini以获取最大上传限制和后限制.

Sul*_*een 59

其他答案仅涉及php.ini directiress.

我想考虑php.ini和Laravel验证规则给出答案

先检查你的 php.ini

对于upload_max_filesize和,post_max_size并更改为您所需的文件大小

upload_max_filesize = 100M
post_max_size = 100M
Run Code Online (Sandbox Code Playgroud)

你应该有这样的规则

public static $updaterules = array(
    'uploadedimage' => 'image|max:5000'            
    );
Run Code Online (Sandbox Code Playgroud)

附加信息 :

你应该检查图像的文件格式

'uploaded' => 'mimes:jpeg,bmp,png'
Run Code Online (Sandbox Code Playgroud)

在此处阅读有关Laravel验证的更多信息

更新:

由于提问者没有注意到服务器正在使用哪个php.ini文件.我正在相应地更新我的答案,所以它可能对将来有类似问题的人有用

查找有关服务器当前使用的php.ini文件的完整配置.

对于Linux

php -i | grep php.ini
Run Code Online (Sandbox Code Playgroud)

对于Windows

php -i | find /i "Configuration File"
Run Code Online (Sandbox Code Playgroud)

来自php文件

<?php
phpinfo();
?>
Run Code Online (Sandbox Code Playgroud)


Jun*_* L. 12

编辑你的php.ini文件并设置:

memory_limit = 32M
upload_max_filesize = 70M
post_max_size = 100M
Run Code Online (Sandbox Code Playgroud)

并重新启动apache.


Che*_*ea 9

好吧,我找到了答案,上面提到的事情要在php.ini中进行更改,upload_max_filesize并且post_max_size实际上是解决问题所提到问题的唯一方法.

唯一的问题是我使用wamp并且显然wamp在php文件夹中有一个php.ini文件,在apache文件夹中有一个.当应该对apache文件夹中的一个进行更改时,我在PHP文件夹中进行了更改.

  • @Chelsea你的答案和'phuti`给出的答案有什么区别,因为你所需要的只是在多个文件中改变`upload_max_filesize`和`post_max_size`.我很惊讶你没有标记他的答案,而是你发布了一个回答他的建议. (2认同)