2015-08-25 64 views
0

我需要一次上傳很多(大約300-400張照片)。我使用Laravel 4.2來做到這一點。laravel多個圖片上傳器無法上傳?

一切正常,除非不上傳。我擁有的一切:

控制器:

編輯

public function postUpload() { 
    // getting all of the post data 
    $files = array('file' => Input::file('file')); 
    //echo "<pre>"; 
    //var_dump($files); 
    //echo "</pre>"; 
    //die; 
    $map = Input::get('mapname'); 
    // setting up rules 
    $rules = array('file' => 'max:10000'); //mimes:jpeg,bmp,png and for max size max:10000 
    // doing the validation, passing post data, rules and the messages 
    $validator = Validator::make($files, $rules); 
    if ($validator->fails()) { 
    // send back to the page with the input data and errors 
    Session::flash('error_message', 'Er ging iets mis!'); 
    return Redirect::to('admin/img/upload')->withInput()->withErrors($validator); 
    } 
    else { 
    // checking file is valid. 

     if(Input::hasFile('file')) 
     { 
      //echo "<pre>"; 
      //var_dump(Input::hasFile('file')); 
      //echo "</pre>"; 
      //die; 
      foreach($files as $file) 
      { 
       $destinationPath = 'public/pictures/overall/'.$map; // upload path 
       $filename = str_random(40).'_'.$file[0]->getClientOriginalName(); 
       $extension = $file[0]->getClientOriginalExtension(); // getting image extension 
       $file[0]->move($destinationPath, $filename); // uploading file to given path 
      } 
     // sending back with message 
     Session::flash('success', 'Succesvol geüpload!'); 
     return Redirect::to('admin/img/upload'); 
     } 

    else { 
     // sending back with error message. 
     Session::flash('error_message', 'Er ging iets mis!'); 
     return Redirect::to('admin/img/upload'); 
    } 
    } 
} 

視圖(渲染):

<form method="POST" action="http://localhost/RPR/admin/img/uploadfile" accept-charset="UTF-8" enctype="multipart/form-data"> 
    <input name="_token" type="hidden" value="92YNpAB9HsmWJm8FbepriZWfy9mjUI2rziVBKJhs"> 
    <select id="mapname" name="mapname"> 
     <option value="TAC-Tielt-Shakedown-2015">TAC Tielt Shakedown 2015</option> 
     <option value="TAC-Tielt-2013">TAC Tielt 2013</option> 
     <option value="Rally-van-Staden-2015">Rally van Staden 2015</option> 
    </select> 
    <br><br> 
    <input multiple="multiple" name="file" type="file"> 
    <br> 
    <button type="submit" class="btn btn-success">Uploaden</button> 
</form> 

的選擇是自動創建的。

有人能找到問題嗎?它什麼都不做。

我在帖子上做了一個var_dump,但那隻給了我token和mapname,沒有圖像......有什麼奇怪的東西?

+0

它是否在'admin/img/upload'中重定向? – aldrin27

+0

你是什麼意思?上傳文件的視圖是'admin/img/upload',然後上傳到'admin/img/uploadfile' – Robin

+0

我的意思是如果它失敗,它會在你的視圖'admin/img/upload'中重定向? – aldrin27

回答

-1
- List item 

The issue is with the input field you are using to upload, I assume you are uploading multiple images 

`<input multiple="multiple" name="file" type="file">` -- this wouldnt work, update to this: 

`<input multiple="multiple" name="file[]" type="file">` 

and also your controller: 

    $files = Input::file('file'); 

    if ($files){ 
    ///upload logic 
    foreach($files as $file) 
       { 
    $destinationPath = 'public/pictures/overall/'.$map; 
    $filename = $file->getClientOriginalName(); 
    $upload_success = $file->move($destinationPath, $filename); 
       } 
      // sending back with message 
      Session::flash('success', 'Succesvol geüpload!'); 
      return Redirect::to('admin/img/upload'); 

    } 

that should work 
+0

然後我得到這個錯誤:'不能使用Symfony \ Component \ HttpFoundation \ File \ UploadedFile類型的對象作爲數組' – Robin

+0

嘗試在打開您的窗體替換這個

'http:// localhost/RPR/admin/img/uploadfile','files'=> true))}} – osleonard

+0

它發佈的東西,exept他只需要最後選擇照片,只上傳那一個... – Robin