Laravel文件上传验证不会触发 - TokenMismatchException

时间:2016-11-10 13:36:16

标签: php laravel file-upload

如果文件符合此验证规则,我可以上传文件

 private void rxPublishProgress()
{
 final  OkHttpClient mOkHttpClient = new OkHttpClient();

  final Request mRequest = new Request.Builder().url(AirportService.SERVICE_ENDPOINT).build();

    Observable.create(new Observable.OnSubscribe<String>()
    {
        @Override
        public void call(Subscriber<? super String> subscriber)
        {
            try {
                InputStream inputStream;
                okhttp3.Response response = mOkHttpClient.newCall(mRequest).execute();
                if (response.isSuccessful())
                {
                    inputStream = response.body().byteStream();
                    long len = response.body().contentLength();

                    String progress = "0";
                    subscriber.onNext(progress);

                   BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
                    StringBuilder sb = new StringBuilder();
                    byte[] data = new byte[1024];
                    long total = 0;
                    int count;
                    String line = null;

                    final int bufferSize = 1024;
                    boolean flag = false;
                    JSONObject jsonObject = null;
                    final char[] buffer = new char[bufferSize];
                    final StringBuilder out = new StringBuilder();
                    Reader in = new InputStreamReader(inputStream, "UTF-8");

                    for(;flag ==false ;)
                    {
                        count = in.read(buffer, 0, buffer.length);

                        if (count == -1)
                        {
                          progress = "100";
                          subscriber.onNext(progress);
                          flag = true;
                        }
                        else
                        {
                            out.append(buffer, 0, count);
                            //  Log.d("out",out.toString());
                            total += count;
                            progress = String.valueOf(total * 100 / len);
                            subscriber.onNext(progress);
                        }
                    }

                    inputStream.close();

                    // try parse the string to a JSON object
                    try
                    {
                        jsonObject = new JSONObject(out.toString());
                    }
                    catch (JSONException e)
                    {
                     e.printStackTrace();
                    }

                    Log.d("lengt",String.valueOf(jsonObject.length()));

                    subscriber.onCompleted();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).subscribeOn(Schedulers.newThread()).subscribe(new Subscriber<String>() {
        @Override
        public void onCompleted()
        {
          Log.d("Complete","complete");
        }

        @Override
        public void onError(Throwable e)
        {
        }

        @Override
        public void onNext(final String progress) {
           getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run()
                {
                 percentage.setText(progress+"%");
                }
            });
        }
    });
一切都很顺利。

我在php.ini中将upload_max_filesize设置为32MB,将post_max_size设置为40MB

但如果我尝试上传大于40MB的文件,我的验证规则甚至不会触发。我得到TokenMismatchException错误....

如果有人可以通过简单地尝试上传一些非常大的文件(例如视频文件)来验证这一点

3 个答案:

答案 0 :(得分:2)

当您超出后期有效负载大小时 - 所有内容都被删除,因此csrf_token不会进入laravel并且上传文件为空,因此无法验证。

更新

要解决此问题,您需要在使用javascript或jquery

上传之前检查文件大小

这是一个例子:

How to check file input size with jQuery?

答案 1 :(得分:1)

在文件上传的情况下,必须将文件复制到服务器的临时位置,然后规则才有效。所以你的服务器不允许大小超过40MB(post_max_size)的文件进入临时位置,所以规则不起作用。 相反,为了解决这个问题,您需要对文件进行前端验证。

您可以使用简单的Javascript执行此操作,如下所示,

$('input[type="file"]').change(function () {
if (this.files[0] != undefined) {
    var name = this.name;
    var filesize = this.files[0].size;
    var field = $('#' + this.name).parents('.input-group');

    //check if file size is larger than 3MB(which is 3e+6 bytes)
    if (filesize > 3000000) {
       alert(filesize);
    //reset that input field if its file size is more than 3MB
       $('[name="' + name + '"]').val('')
    }
  }
});

您可以在type =&#39; file&#39;的所有输入中包含此内容。通过以字节为单位更改大小限制。

答案 2 :(得分:0)

我遇到了同样的问题。您还应该检查验证器是否正在检查file数据,而不是post数据:

我已经测试过:

$validator = Validator::make($request->post(), [
    myfield' => 'required|image|mimes:jpeg,png,jpg,gif|max:1000000'
]);

应该是:

$validator = Validator::make($request->file(), [
    myfield' => 'required|image|mimes:jpeg,png,jpg,gif|max:1000000'
]);