Laravel 4:上传后移动文件

时间:2014-12-27 14:39:08

标签: php laravel laravel-4

我在尝试上传我的应用上的照片和任何类型的文件时遇到问题,因为上传但作为.tmp文件并且它们在我的视图中无法正常显示

1.-我的表格,我试图上传一个包含姓名,小组,电子邮件,描述和照片的会员

  {{Form::open(array('action' => 'AdminController@addMember','files'=>true)) }}
  {{ Form::label('file','Agregar Imagen',array('id'=>'','class'=>'')) }}
  {{ Form::file('file','',array('id'=>'','class'=>'')) }}
  <br/>
  {{Form::text('name','',array('class' => 'form-control','placeholder'=> 'Nombre'))}}
  {{Form::text('group','',array('class' => 'form-control','placeholder'=> 'Cargo'))}}
  {{Form::text('email','',array('class' => 'form-control','placeholder'=> 'Correo'))}}  
  {{Form::textarea('description','',array('class' => 'form-control','placeholder'=>''))}}
  <!-- submit buttons -->
  {{ Form::submit('Guardar') }}          
  <!-- reset buttons -->
  {{ Form::reset('Reset') }}          
  {{ Form::close() }}

2. - 我在控制器中的上传功能

public function addMember()
{
    $name = Input::file('file')->getClientOriginalName();
    $newname = Input::file('file')->getFilename();    
    Input::file('file')->move(storage_path(),$name);
    $subpath = storage_path();
    $path = $subpath.'/'.$newname2;
    $name2 = Input::get('name');
    $email = Input::get('email');
    $description = Input::get('description');
    $group = Input::get('group');

    DB::table('contactgroups')->insert(
        array('group' => $group, 'name' => $name2, 'path' => $path, 'email' => $email, 'description' => $description)
    );
    $members = DB::table('contactgroups')->get();
    return View::make('admin.members',['members' => $members]);
} 

我知道我应该使用模型在我的数据库上传内容,但现在不是问题

3.-我的显示视图

@extends('layouts.main')
@section('content')
    @foreach($members as $member)   
        <div class = "row fondue">
                <h3><div class="col-md-12"><b><?=$member->name ?></b></div></h3>    
                <div class="col-md-4"> <img src="<?=$member->path ?>" alt="Image" class = "contact-img"></div>
                <div class="col-md-4"><?=$member->description ?></div>
                <div class="col-md-4"><?=$member->email ?></div>

        </div>  

    @endforeach

@stop

并且全部...信息保存在数据库中,但图像在视图上显示不正确,文件上传为tmp文件,我不知道为什么

1 个答案:

答案 0 :(得分:1)

来自Laravel文档

移动上传的文件

Input::file('photo')->move($destinationPath);

Input::file('photo')->move($destinationPath, $fileName);

来源:http://laravel.com/docs/4.2/requests#files

相关问题