2015-11-14 103 views
2

我在laravel 5工作,我想上傳用戶的個人資料圖片。 我的上傳工作正常,但我不知道如何將圖像傳到文件夾。 有人可以幫助我嗎?上傳圖像和訪問laravel 5

控制器

public function updateProfile() { 
    $profileData = Input::except('_token'); 
    $validation = Validator::make($profileData, User::$profileData); 
    if ($validation->passes()) { 
     $file = array_get($profileData,'imagem'); 
     $destinationPath = 'imagens/perfil'; 
     $extension = $file->getClientOriginalExtension(); 
     $fileName = rand(11111, 99999) . '.' . $extension; 
     $upload_success = $file->move($destinationPath, $fileName); 
     User::where('id', Input::get('id'))->update($profileData); 
     return view('backend/perfil.index')->with('message', 'Updated Succesfully'); 
    } else { 
     return view('backend/perfil.index')->with('message', $validation->messages()); 
    } 
} 

路線

Route::post('backend/perfil', '[email protected]'); 

指數

<div class="col-md-3 col-lg-3 " align="center"> 
     <img alt="User Pic" src="{{ Auth::user()->imagem}}" class="img-circle img-responsive"> 
    </div> 


{!! Form::open(array('class' => 'form-horizontal', 'url' => 'backend/perfil', 'name' => 'updateProfile', 'role' => 'form', 'files'=> true))!!} 

     <input type="hidden" name="id" value="{{Auth::user()->id}}"> 


     <div class="row" style="margin-bottom: 20px;"> 
      <div class="col-md-3 col-lg-3"></div> 
      <div class="col-md-2 col-lg-2"> 
       {!! Form::label('imagem', 'Imagem', ['class' => 'label_perfil']) !!} 
      </div> 
      <div class="col-md-5 col-lg-5"> 
       {!! Form::file('imagem', ['class' => 'input-file']) !!} 
      </div> 
     </div> 
     <div class="row" style="margin-bottom: 20px; margin-top: 30px;"> 
      <div class="col-md-3 col-lg-3"></div> 
      <div class="col-md-9 col-lg-9"> 
       {!! Form::submit('Alterar perfil', ['class' => 'btn btn-primary']) !!} 
      </div> 
     </div> 
    {!! Form::close() !!} 
+0

'如何獲取圖像到folder'這意味着你不知道如何檢索或者你有問題存儲圖像本身? –

+0

你是什麼意思?我的問題是你不能在文件視圖中獲取圖像 –

+0

你的意思是列出所有的圖像,如「預覽」或獲取單頁圖像? –

回答

1

從OP的評論我想這是運不在數據庫中保存圖像的路徑。

你怎麼能做到這一點

第1步:

每當保存圖像,將圖像保存的路徑在數據庫中的一些身份。

步驟2:

從數據庫中檢索圖像的路徑和具有它的img src標籤

$yourImagePath = 'xyz.jpg'; # Retrieved from DB

然後

<img src='<?php echo $yourImagePath?>'>

內部
+0

我沒有字段在數據庫調用圖像。 最好只在數據庫中創建一個字段的方式? –

+0

是的,別無他法,因爲我們需要以某種獨特的方式來識別圖像。我們可以」簡單地顯示從資產文件夾中的圖片:);) –

+0

我會試試:)感謝 如果它出了問題我在這裏寫 –

-1

,而不是這一行

$destinationPath = 'imagens/perfil'; 

只是嘗試下面的代碼

$destinationPath = publicpath().'imagens/perfil'; 
+0

什麼錯誤,我已經做了馬丁施耐德? –