2016-02-01 1 views
1

Моя форма как следоватьКак отправлять файлы в Laravel

{{ Form::open(array('enctype' => 'multipart/form-data'))}} 

    <div class="vendor-box-wrap"> 
     <div class="page3-suggested-cat-wrap" style="float: left;width: 100%;border-bottom: 1px dotted;padding-bottom: 10px;margin-left: 20px;"> 
     <label style="width:9%;" for="9009" class="page3-suggested-cat-label" >BoQ</label> 
     <div class="input-group" style="margin-top: 10px; 
     width: 70%;"> 
     <span class="form-control"></span> 
     <span class="input-group-btn"> 
     <span class="btn btn-primary" onclick="$(this).parent().find('input[type=file]').click();">Browse</span> 
     <input name="file|90009|107" id="file|9009" 
     value="" onchange="$(this).parent().parent().find('.form-control').html($(this).val().split(/[\\|/]/).pop());" 
     style="display: none;" type="file"> 
     </span> 

    </div> 
    </br> <center><h2><strong>OR</strong></h2></center> 
    </div> 
    </div> 

    <div class="next-btn-wrap"><div class="cf"></div> 
    <div class="back-btn-wrap"> 
     {{ Form::submit('Back',array('class' => 'back-btn', 'name' => 'back'))}} 
    </div> 
    <div class="save-btn-wrap"> 
     {{ Form::submit('Save',array('class' => 'save-btn','name' => 'save'))}} 
    </div> 
    {{ Form::submit('Next',array('class' => 'next-btn','name' => 'next'))}} 
</div> 
{{ Form::close()}} 

и в моем контроллере я использую следующий код, чтобы получить данные

$aa = Input::except(array('_token','back','save','next')); 
      //dd($aa); 
      foreach ($aa as $key=>$value){ 

       $ids = explode("|",$key); 

       if(isset($ids[0]) && $ids[0]=="file"){ 

        $userid = Session::get('userid'); 
        $event = Session::get('event'); 
        $fileTblObj = new fileHandler(); 
        $ans = Answer::find($ids[2]); 

        if(isset($aa[$key])){ 
         //dd($aa[$key]); 
        if(Input::file($key)->isValid()) { 

          $destinationPath = 'app/uploads/'.$event.'/'.$userid.'/'.$pageNo ; // upload path 
          $extension = Input::file($key)->getClientOriginalExtension(); // getting image extension 
          $name = Input::file($key)->getClientOriginalName(); 
          $curFilesize = Input::file($key)->getClientSize(); 
          $mime =Input::file($key)->getMimeType(); 

          if (!File::exists($destinationPath."/boq-".$name)){ 

           //creating details for saving inthe file_handler Table 
            $fileTblObj->user_id = $userid; 
            $fileTblObj->eventName = $event ; 
            $fileTblObj->fileName = "boq-".$name; 
            $fileTblObj->formPage =$pageNo ; 
            $fileTblObj->filePath = $destinationPath."/"; 
            $fileTblObj->mime= $mime; 
            $ans->answer_text = 'Yes'; 


            Input::file($key)->move($destinationPath, "boq-".$name); // uploading file to given path 
            //Input::file($key)->move($boqPath, $boqname); // uploading file to given path 
            //Save filedetails 
            $fileTblObj->save(); 
            $ans->save(); 
            Session::flash('success', 'Upload successfully'); 

          }else if(File::size($destinationPath."/".$name) != $curFilesize){ 

            $fileDtls = $fileTblObj->where('uid',$userid)->where('fileName',$name)->where('formPage',$pageNo)->first(); 
            Input::file($key)->move($destinationPath, $name); 
            $ans->answer_text = 'Yes'; 
            $ans->save(); 
            $fileTblObj->where('id',$fileDtls->id)->update(array('updated_at'=>date("Y-m-d h:m:s",time()))); 
          } 
          //return Redirect::to('upload'); 
         } 

        } 
        else 
        { 
         if($ans->answer_text =='') 
         { 
          $ans->answer_text = 'No'; 
          $ans->save(); 
         } 
        } 

       } 

Моя проблема в том, что я не в состоянии получить файловые детали на серверную если заявление

if(isset($ids[0]) && $ids[0]=="file"){ 

} 

всегда ложно.

Любая идея Как я могу это исправить. Я также попытался изменить функцию формы для

{{ Form::open(array('files' => true)) }} 

Все еще его не показывает информацию о файле

ответ

0

Чтобы отправить файл, я лично использую эти методы.

Вид:

{!! Form::open(array('action' => '[email protected]', 'method' => 'POST', 'files'=>true)) !!} 
    {!! Form::file('thefile') !!} 
    {!! Form::submit('Save', array('class' => 'btn')) !!} 
{!! Form::close() !!} 

Контроллер:

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

Надеется, что это помогает!

Смежные вопросы