如何正确存储头像
(在开发者模式下)我现在拥有Controller的方式 它将新的图像路径存储到数据库中,但不会将其上传到“上传/头像”中。夹。
此外,当我尝试在不上传新头像的情况下编辑个人资料时,它会抛出ErrorException undefined variable:avatar
,
我的托管服务器上的版本如果没有上传但只编辑个人资料,也会抛出Errorexception,
如果我尝试上传新的头像,它会告诉我
NotWritableException
Can't write image data to path (/home/vlindr.com/vlindr/public/uploads/avatars/1504691841.jpg)
有人知道如何解决这个问题吗?
我的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use Session;
use Image;
use Illuminate\Support\Facades\File;
use App\User;
use DB;
use Illuminate\Support\Facades\Storage;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
}
//
public function index(){
return view('profiles.profile', array('user' => Auth::user()) );
}
public function edit()
{
return view('profiles.edit')->with('info', Auth::user()->profile);
}
public function update(Request $request)
{
$this->validate($request, [
'location' => 'required',
'about' => 'required|max:355',
'passion' => 'required|max:355'
]);
Auth::user()->profile()->update([
'location' => $request->location,
'about' => $request->about,
'passion' => $request->passion
]);
$user = User::find(Auth::user()->id);
// Handle the user upload of avatar
if ($request->hasFile('avatar')) {
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
}
// Delete current image before uploading new image
if ($user->avatar !== 'man.png' && $user->avatar !== 'woman.png')
{
$file = public_path('/uploads/avatars/' . $user->avatar);
if (File::exists($file)) {
unlink($file);
}
}
Image::make($avatar->getRealPath())->resize(350, 350)->save( public_path('/uploads/avatars/' . $filename ) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
return back()->with('msg', 'Profiel is bijgewerkt');
}
}