在laravel 5中上传文件超过2M

问题描述 投票:1回答:2

我在laravel 5中有一个上传表单,我想发送一个大于2M的文件,并且我更改了php.ini以获得最大上传大小和发布大小,但是它也确实起作用!!当启用我的csrf时,我得到了csrf令牌不匹配,而当我以表格形式发送所有字段后将其禁用后,发送给控制器为空!我的看法:

<div class="col-lg-6" style="float:right">
{!! Form::open(array('url'=>'admin/uploadtmp','method'=>'POST', 'files'=>true)) !!}


                            <div class="form-group col-md-12 pull-right">

                                 {!! Form::text('title', Input::old('title'), array('class' => 'form-control', 'placeholder' => 'نام قالب')) !!}

                                    <div style="color:red">
                                        {!!$errors->first('title')!!}
                                    </div>
                            </div>

                            <div class="form-group col-md-12">
                                {!! Form::textarea('description', Input::old('description'), array('class' => 'form-control', 'rows' =>'8', 'placeholder' => 'توضیحات')) !!}

                                <div style="color:red">
                                        {!!$errors->first('description')!!}
                                    </div>
                            </div>
                            <div class="form-group col-md-6" style="float:right">

                                <select name="brandid" class="form-control" placeholder="برند قالب">
                                    <option value="">برند قالب خود را انتخاب کنید</option>
                                    @foreach($brands as $brand)
                                    <option value="{{$brand->id}} ">{{$brand->title}} </option>
                                    @endforeach
                                  </select>
                            </div>
                            <div class="form-group col-md-6" style="float:right">
                               <select name="categoryid" class="form-control">
                                    <option value="">دسته بندی قالب خود را انتخاب کنید</option>
                                    @foreach($categories as $category)
                                    <option value="{{$category->id}} ">{{$category->title}} </option>
                                    @endforeach
                                  </select>
                            </div>
                            </div> 

                            <div class="form-group col-md-6">
                                <div class="form-group">
                                     {!! Form::label('لطفا فایل قالب خود را به صورت زیپ شده از طریق دکمه ی زیر انتخاب کنید') !!}    
                                     {!! Form::file('zip' , Input::old('zip')) !!}
                                     <div style="color:red">
                                        {!!$errors->first('zip')!!}
                                    </div>
                                </div>
                                <div class="form-group">
                                     {!! Form::label('لطفا فایل اسکرین شات قالب خود را با فرمت jpg و حجم کمتر از پنجاه کیلوبایت از طریق دکمه ی زیر انتخاب کنید') !!}
                                     {!! Form::file('image') !!}
                                     <div style="color:red">
                                        {!!$errors->first('image')!!}
                                    </div>
                                </div>

                                <input type="submit" name="submit" class="btn btn-primary pull-left" value="ارسال" >


                        {!! Form::close() !!}

                        @if(Session::has('error'))
                            <div class="col col-lg-12 alert alert-success alert-dismissable"> 
                                <button style="float:left" type="button" class="close" data-dismiss="alert" aria-hidden="true">&times; </button>
                                    {!! Session::get('error') !!}
                            </div>
                        @endif
                        @if(Session::has('success'))
                            <div class="col col-lg-12 alert alert-success alert-dismissable"> 
                                <button style="float:left" type="button" class="close" data-dismiss="alert" aria-hidden="true"> &times; </button> 
                                    {!! Session::get('success') !!}
                            </div>
                        @endif
                        </div>
              </div>

我的控制器:

class TemplateController extends Controller
{
    public function newtemplate(){
        // getting all of the post data
        $files = array(
          'zip'   => Input::file('zip'),
          'image' => Input::file('image'),
          'title' => Input::input('title'),
          'description' => Input::input('description'),
          'brandid' =>Input::input('brandid'),
          'categoryid' =>Input::input('categoryid'),
          );
        $rules = array(
          'image' => 'mimes:jpeg,jpg | max:5000 | required',
          'zip'   => 'mimes:zip,rar | max:40000 | required',
          'title'   => 'required',
          'description'   => 'required',
          ); //mimes:jpeg,bmp,png and for max size max:10000
         $messages =[

              'image.mimes' => 'فرمت فایل ارسالی شما صحیح نمی باشد',
              'image.max' => 'حداکثر اندازه ی فایل ارسالی شما باید 50 کیلو بایت باشد',
              'zip.mimes' => 'فرمت فایل ارسالی شما باید حتما zip باشد',
              'required' => 'این فیلد نباید خالی باشد'
          ];
          $validator = Validator::make($files, $rules,$messages);
          if ($validator->fails()) {
            // send back to the page with the input data and errors
            return Redirect::to('admin/templates')->withInput()->withErrors($validator);
          }
          else{
                if (Input::file('zip')->isValid() && Input::file('image')->isValid()) {


                      $template = new Template;
                      $template->creator_id = Auth::user()->id;
                      $template->title = Input::input('title');
                      $template->description = Input::input('description');
                      $template->category_id = Input::input('categoryid');
                      $template->brand_id = Input::input('brandid');
                      $template->save();

                      $lastid = $template->id;

                      //$destinationPath = 'uploads'; // upload path
                      $extension = Input::file('zip')->getClientOriginalExtension(); // getting image extension
                      $extension2 = Input::file('image')->getClientOriginalExtension(); // getting image extension
                      $filename = $lastid.'.'.$extension; // renameing image
                      $filename2 = $lastid.'.'.$extension2; // renameing image

                      //Storage::disk('local')->put('screenshots/'.$filename, Input::file('image'));
                      Input::file('zip')->move(storage_path().'/zipfiles', $filename);
                      Input::file('image')->move(storage_path().'/screenshots', $filename2);
                      //Storage::disk('local')->put('zipfiles/'.$filename2,Input::file('zip'));
                      // sending back with message
                      Session::flash('success', 'عملیات با موفقیت صورت گرفت'); 
                      return Redirect::back();

              }

               else {
                  // sending back with error message.
                  Session::flash('error', 'مشکل در آپلود فایل ها');
                  return Redirect::back();
                }
            }
       }
}
laravel upload csrf-protection large-file-upload
2个回答
0
投票

您需要在php.ini配置中更改upload_max_filesizepost_max_size,该配置的位置取决于您的环境,但是您可以将一个Loaded Configuration File变量包含该文件的完整路径。 ini,然后您更改

upload_max_filesize = 50M
post_max_size = 50M

最后重新启动apache和voilà

您可以查看this question了解更多详细信息,这不是laravel问题,而是php配置


0
投票

首先,您是否通过命令php artisan serve运行应用程序?

如果是,则应从/etc/php-<version>/cli/而不是/etc/php-<version>/apache/修改您的php.ini文件。因为此命令从cli运行,所以它使用自己的php配置。

因此请使用geditsublime打开任何您喜欢的东西。并更改upload_max_filesizepost_max_sizememory_limit

memory_limit = 1024M
upload_max_filesize = 512M
post_max_size = 512M

然后通过service apache2 restart重新启动。

© www.soinside.com 2019 - 2024. All rights reserved.