dongme8388 2018-08-16 09:53
浏览 33
已采纳

使用干预在laravel上载图像

i am tring to upload images in laravel 5.6 using intervention and its working perfectly, the only problem is the path it is storing on the database is like /var/www/Laravel/project/public/uploads/students/1534413021.jpg and what i want is for it to store it as /uploads/students/1534413021.jpg. Here is my form

    <form class="form-horizontal" method="post" action="{{ route('student.Avatar.submit',$student->id)}}" enctype="multipart/form-data">
                  {{ csrf_field() }}
                  <div class="form-group ">
                    <label class="col-sm-2 control-label">Select Image To Upload:</label>
                    <div class="col-sm-10 {{ $errors->has('avatar') ? ' has-error' : '' }}">
                      <input type="file" name="avatar" value="{{ old('avatar') }}" class="form-control rounded">
                      @if ($errors->has('avatar'))
                      <span class="help-block">
                        <strong>{{ $errors->first('avatar') }}</strong>
                      </span>
                      @endif
                    </div>
                  </div>
                  <div class="form-group">
                    <div class="col-sm-4 col-sm-offset-2">
                      <button type="submit" class="btn btn-primary btn-rounded btn-block">Save changes</button>
                    </div>
                  </div>
                </form>

and here is the controller

      public function uploadAvatar(Request $request, Student $student)
  {
    $validatedData = $request->validate([
      'avatar' => 'required | mimes:jpeg,jpg,png,bmb,gif,svg',
    ]);

    if ($request->hasFile('avatar')) {
      $image = $request->file('avatar');
      $fileName = time() . '.' . $image->getClientOriginalExtension();
      $location = public_path('uploads/students/'.$fileName);
      Image::make($image)->resize(800,400)->save($location);

      $studentUpdate = Student::where('id',$student->id)
      ->update([
        'avatar'=>$location,
      ]);
      if ($studentUpdate) {
        return redirect()->route('students.profile',$student->id)->with('success','Student Profile Created Successfully');
      }else{
          return back()->withInput()->with('errors','Error adding new parent.');
      }

    }
    return back()->withInput()->with('errors','PLease Select An Image.');
  }
  • 写回答

2条回答 默认 最新

  • douxiandiyo58855 2018-08-16 09:59
    关注

    change your path while saving like this:

    $location = public_path('uploads/students/'.$fileName); 
    

    To this:

    $location = 'uploads/students/'.$fileName;
    

    It will automatically go to public/$location and save the file and you will have only required string in database;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog