dqmq0654 2016-06-29 07:26
浏览 133

如何在laravel 5.2中上传图片

This is my form

@extends('layout.template')
    @section('content')
        <h1>Add Student</h1>
        {!! Form::open(array('action' => 'studentController@save', 'files'=>true)) !!}

        <div class="form-group">
            {!! Form::label('Profile-Picture', 'Profile Picture:') !!}
            {!! Form::file('image',null,['class'=>'form-control']) !!}
        </div>

        <div class="form-group">
            {!! Form::submit('Save', ['class' => 'btn btn-primary form-control']) !!}
        </div>
        {!! Form::close() !!}
    @stop

This is my controller method

public function save()
{
    $students=Request::all();
    students::create($students);
    Session::flash('flash_message', 'Record successfully added!');
    return redirect('students');
}

when i upload image and submit image than in the database column field save this image address "/tmp/phpFFuMwC";

  • 写回答

2条回答 默认 最新

  • dor65412 2016-06-29 07:39
    关注

    That' s because you are saving the temporarily generated file url.

    For files you need to manually save it to the desired location ( make sure it passes validations ):

    $request->file('photo')->move($destinationPath);
    
    // With a custom filename    
    $request->file('photo')->move($destinationPath, $fileName);
    

    and then store the new filename ( with or without the path ) in the database something like this:

    $students = new Students;
    $students->image = $fileName;
    ...
    $students->save();
    

    Docs: https://laravel.com/docs/5.2/requests#files

    评论

报告相同问题?

悬赏问题

  • ¥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系统搭建请教(跨境电商用途)