使用干预在laravel中上传图像

时间:2018-08-16 09:53:32

标签: php laravel intervention

我正试图通过干预在laravel 5.6中上传图像,并且它可以正常工作,唯一的问题是它在数据库中存储的路径像/var/www/Laravel/project/public/uploads/students/1534413021.jpg一样,我想要的是将其存储为{ {1}}。     这是我的表格

/uploads/students/1534413021.jpg

2 个答案:

答案 0 :(得分:0)

在保存时更改路径,如下所示:

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

为此:

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

它将自动转到public/$location并保存文件,您在数据库中将只包含所需的字符串;

答案 1 :(得分:0)

您应该尝试以下操作:

 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/');
      $uploadImage = Image::make($image->getRealPath())->resize(800,400);
      $uploadImage->save($location.'/'.$fileName);

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

      $studentUpdate = Student::where('id',$student->id)
      ->update([
        'avatar'=>$imageName,
      ]);
      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.');
  }