2015-11-14 100 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

什么错误,我已经做了马丁施耐德? –