dongyi9330 2016-02-01 13:42 采纳率: 100%
浏览 32

如何在Laravel中提交文件

My form is as follow

    {{ 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()}}

and in my controller I am using following code to get the data

$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();
                        }
                    }

                }

My problem is I am not able to get the file details on the back-end the if statement

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

}

is always false .

Any Idea How I can fix this. I also tried changing the The FOrm function to

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

Still its not showing the file details

  • 写回答

1条回答 默认 最新

  • duanjiong2021 2016-02-01 15:50
    关注

    To send a file, I personally use this methods.

    View:

    {!! Form::open(array('action' => 'TestController@store', 'method' => 'POST', 'files'=>true)) !!}
      {!! Form::file('thefile') !!}
      {!! Form::submit('Save', array('class' => 'btn')) !!}
    {!! Form::close() !!}
    

    Controller:

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

    Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)