2016-07-24 58 views
1

Im與Laravel 5的表單有問題。當我將enctype屬性指定爲'multipart/form-data'時,我收到一個令牌不匹配錯誤。如果它被刪除,表單總是失敗我的控制器中指定的驗證。Laravel 5:上傳多個文件和其他輸入

HTML

<form class="lajax" action="{{ action('[email protected]') }}" method="POST"> 
        <div class="form-group"> 
         <label>Album Name</label> 
         <input type="text" name="name" class="form-control">             
        </div> 

        <div class="form-group"> 
         <label for="coverFile">Album Cover Image</label> 
         <input name="cover" type="file" id="coverFile"> 
         <p class="help-block">Example block-level help text here.</p> 
        </div> 

        <div class="form-group"> 
         <label for="albumFiles">Album Images</label> 
         <input type="file" name="photos[]" multiple> 
        </div> 

        <button type="submit" class="btn btn-primary">Create Album</button> 

        {{ csrf_field() }} 
       </form> 

控制器

public function store(Request $request) 
    { 

     //request input verification rules 
     $rules=[ 
      'name'=>'required', 
      'cover'=>'required|image', 
      'photos'=>'required|array', 
      'photos.*'=>'image' 
     ]; 

     //perform validation 
     $this->validate($request,$rules); 

     // blah blah 
    } 

具體而言,圖像似乎是失敗。

錯誤報告:封面不是圖像,照片0不是圖像,照片1不是圖像.....等等。

請幫

回答

0

我發現了錯誤!它在我的php.ini文件中。我將3M的post_max_size改爲1000M。有效。

0

變化:

<form class="lajax" action="{{ action('[email protected]') }}" method="POST"> 

要:

<form method="POST" action="{{ action('[email protected]') }}" accept-charset="UTF-8" enctype="multipart/form-data"> 

在你的控制器,你可以檢查你的輸入這樣的:

$request->hasFile('file_input_name'); 

還要檢查Laravel Collectiv e創建表格:https://laravelcollective.com/