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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28529518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:55:16  来源:igfitidea点击:

Small file get uploaded but not large file in Laravel

phplaravel

提问by Chelsea

I have this action in controler

我在控制器中有这个动作

    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";
    }
  }
}

It works for small file size like 2MB but won't work for 4MB file size. For 4MB or more it gets error in validation.In the code above there's this code

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

 if ($validator->fails()) {

       return "error validation";
      }

It gives this custom error error validation. I have already worked on configuring php.ini for max upload limit and post limit.

它给出了这个自定义错误error validation。我已经在为最大上传限制和发布限制配置 php.ini。

采纳答案by Chelsea

Well I found the answer, The things mentioned above to make change in php.ini about upload_max_filesizeand post_max_sizeare infact the only thing to solve the problem mentioned by the question.

好吧,我找到了答案,上面提到的在约php.ini中做出改变的事情upload_max_filesizepost_max_size是逸岸来解决问题中提到的问题的唯一的事情。

The only problem was that I am using wamp and apparently wamp has multiple php.ini files one in php folder and one in apache folder. I made changes in PHP folder when the change should be made to the one inside apache folder.

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

回答by Sulthan Allaudeen

Other answers refers saying only about the php.inidirectiress.

其他答案仅指有关php.ini女董事的说法。

I would like to give answer considering both php.ini and the Laravel Validation Rule

我想同时考虑 php.ini 和Laravel Validation Rule给出答案

First check your php.ini

首先检查你的 php.ini

For the upload_max_filesizeand post_max_sizeand change to your desired file size

对于upload_max_filesizepost_max_size并更改为您想要的文件大小

upload_max_filesize = 100M
post_max_size = 100M

You shall have your rule like this

你会有这样的规则

public static $updaterules = array(
    'uploadedimage' => 'image|max:5000'            
    );

Additional information :

附加信息 :

You shall check for the file format for image by

您应通过以下方式检查图像的文件格式

'uploaded' => 'mimes:jpeg,bmp,png'

Read more about Laravel validation here

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

Update :

更新 :

As the questioner didn't noticed which php.ini file is being used by the server. I am updating my answer accordingly so it might be useful for someone who has similar problems in future

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

To find the full configuration about the php.ini file which is currently being used by your server.

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

For Linux

对于 Linux

php -i | grep php.ini

For Windows

对于 Windows

php -i | find /i "Configuration File"

From a php file

从一个 php 文件

i.e.,

IE,

<?php
phpinfo();
?>

回答by Junius L.

edit your php.inifile and set:

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

memory_limit = 32M
upload_max_filesize = 70M
post_max_size = 100M

and restart apache.

并重新启动apache。

回答by RITHU

FOR WAMP SERVER: Go to /wamp64/bin/apache/apache(your version)/bin/and open php.symlink(.ini)and change
post_max_size,memory_limit,upload_max_filesizeas per your need. and restart the wamp server.

FOR WAMP SERVER:根据您的需要/wamp64/bin/apache/apache(your version)/bin/打开php.symlink(.ini)并更改
post_max_size,memory_limit,upload_max_filesize。并重新启动 wamp 服务器

回答by Mwirabua Tim

For laravel apps running on nginx..

对于在 nginx 上运行的 Laravel 应用程序..

  1. Identify your php ini file:

    php -i | grep php.ini

  2. Edit both the cli and the fpm files to be sure:

    vim /etc/php/7.2/cli/php.ini

    vim /etc/php/7.2/fpm/php.ini

  3. Change these to your prefered limits:

    post_max_size = 1G

    upload_max_filesize = 1G

    memory_limit = 1G

  4. Restart php fpm

    service php7.2-fpm restart

  5. Reload nginx

    service nginx reload

  1. 识别您的 php ini 文件:

    php -i | grep php.ini

  2. 编辑 cli 和 fpm 文件以确保:

    vim /etc/php/7.2/cli/php.ini

    vim /etc/php/7.2/fpm/php.ini

  3. 将这些更改为您喜欢的限制:

    post_max_size = 1G

    上传最大文件大小 = 1G

    内存限制 = 1G

  4. 重启php fpm

    服务 php7.2-fpm 重启

  5. 重新加载nginx

    服务 nginx 重新加载

回答by vitalik_74

Check max_file_uploadsand max_post_sizein php.ini. It shows from phpinfo()

检查max_file_uploadsmax_post_sizephp.ini。它显示从phpinfo()