2016-01-25 51 views
-1

當我保存,我會得到這個錯誤如何使用Laravel Framwork保存並顯示PDF,Excel和Word文件?

未定義的變量:文件

真的,我不知道如何來存儲文件,但我在谷歌查了一些博客,這是我的存儲功能:

public function store(Request $request) 
    { 
     $this->validate($request, $this->rules); 
     if($request->hasFile('file')){ 
      $file = $request->file('file'); 
      $path = $file->getRealPath(); 
      $files = file_get_contents($path); 
     } 
     $input = $request->all(); 
     $charge = $request->get('charge'); 
     $date = $request->get('date'); 
     $job = $request->get('job_id'); 
     $id = \Auth::id(); 
     $current_id = DB::table('charges')->max('invoice_no'); 
     $data = array(); 
     foreach ($charge as $key=>$value){ 
      $data[] = [ 
       'date' => $date, 
       'charge'=>$value, 
       'job_id'=>$job, 
       'user_id'=>$id, 
       'amount'=>$input['amount'][$key], 
       'category'=>$input['category'][$key], 
       'file'=>$files[$key], // here i will get error 
       'invoice_no' => $current_id + 1, 
      ]; 
     } 

我正在創建我們的公司應用程序,所以我們要主持雲。我很困惑將這些文件存儲在我的數據庫或磁盤但我們必須訪問這些文件。而在我的節目視圖中,我想要顯示名稱這個文件,當我點擊該名稱時,我想要顯示那個特定的文件,這怎麼可能?

編輯 這是我的形式

{!! Form::model(new App\Charge, ['route' => ['charges.store'],'class'=>'form-horizontal','role'=>'form']) !!} 

    <div class="form-group"> 
     {!! Form::label('date', 'DATE :',['class'=>'col-md-2 control-label']) !!} 
     <div class="col-md-7"> 
      {!! Form::date('date',\Carbon\Carbon::now(),null,['class' => 'form-control']) !!} 
     </div> 
    </div> 
    <div class="form-group"> 
     {!! Form::label('job_id', 'JOB ID :',['class'=>'col-md-2 control-label']) !!} 
     <div class="col-md-7"> 
      {!! Form::select('job_id',$job,null,['class' => 'form-control']) !!} 
     </div> 
    </div> 
    <div class="form-group"> 
     {!! Form::label('charges', 'CHARGES :',['class'=>'col-md-2 control-label']) !!} 
     <div class="col-md-10"> 
      <div class="input_fields_wrap"> 
       <button class="add_field_button" value="">Add More Charges</button> 
       <div> 
        <input type="text" name="charge[]"> 
        <input type="text" name="category[]"> 
        <input type="text" name="amount[]"> 
        <input type="file" name="file[]"> 
       </div> 
      </div> 
     </div> 
    </div> 


    <div class="form-group"> 
     <div class="col-md-2 col-md-offset-8"> 
      <submitfiled> 
       {!! Form::submit(['class'=> 'btn btn-primary form-control']) !!} 
      </submitfiled> 
     </div> 
    </div> 
{!! Form::close() !!} 
+2

可以通過添加'DD($文件)調試;'行後7和更新您的問題嗎? –

+0

是同樣的錯誤,我使用$文件即時獲取錯誤,該行**未定義變量:文件** – Hamelraj

+0

請提供用於發起請求的窗體的HTML標記。 – Bogdan

回答

1

您正在試圖如果有一個上傳的文件訪問$files變量,只設置:

if($request->hasFile('file')){ 
    $file = $request->file('file'); 
    $path = $file->getRealPath(); 
    $files = file_get_contents($path); 
} 

所以它可能是可能的,你AREN上傳文件。檢查您的表單是否有enctype="multipart/form-data"。另外,還要確保你通過檢查其上線2上傳文件:

dd($request->file('file')); 

比方說,文件的上傳工作,你將整個文件讀入stringfile_get_contents()並將其分配給$files

但在後面的代碼,你認爲$filesarray

'file'=>$files[$key], // here i will get error 

嘗試調試您的變量,看看什麼是真正的在其中。

另外,我會將文件複製到上傳目錄,只保存到這個文件的路徑而不是整個文件的字符串。

+0

我只是試過'dd($ request-> file('file'));'null out爲什麼呢? – Hamelraj

+0

因爲沒有上傳文件。你的'

'有'enctype ='multipart/form-data「'標籤嗎?表單中的文件輸入是否具有名稱'file'? –

+0

我添加加密在我的形式我檢查dd()現在即時通訊脫離那個文件deatils但getRealPath(),getClientOriginalName()不能acces爲什麼呢? – Hamelraj

相關問題