Laravel: 将图片上传到 public/images - 错误的文件名
Laravel: Upload picture to public/images - wrong file name
我正在尝试将上传的图片保存在 public/images 目录中。
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg|max:5048'
]);
$newImageName = uniqid() . '-' . $request->title . '.' . $request->image->extension();
$request->image->move(public_path(('images'), $newImageName));
Post::create([
'title' => $request->input('title'),
'description' => $request->input('description'),
'slug' => SlugService::createSlug(Post::class, 'slug', $request->title),
'image_path' => $newImageName,
'user_id' => auth()->user()->id
]);
return redirect('/blog')
->with('message', 'Dein Beitrag wurde erstellt.');
}
一切正常 - 除了文件名。它的名称应该是 $newImageName,但看起来像 'php51F7.tmp'。此外,扩展名是“.tmp”而不是“.png”或“.jpg”。
非常感谢您的帮助!
PS: 我是Laravel的绝对初学者,请耐心等待
您的代码中有错字
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg|max:5048'
]);
$newImageName = uniqid() . '-' . $request->title . '.' . $request->image->extension();
#Notice here, move() accepts two parameteres
$request->image->move(public_path('images'), $newImageName);
Post::create([
'title' => $request->input('title'),
'description' => $request->input('description'),
'slug' => SlugService::createSlug(Post::class, 'slug', $request->title),
'image_path' => $newImageName,
'user_id' => auth()->user()->id
]);
return redirect('/blog')
->with('message', 'Dein Beitrag wurde erstellt.');
}
是额外圆括号的问题
替换
$request->image->move(public_path(('images'), $newImageName));
有
$request->image->move(public_path('images'), $newImageName);
我正在尝试将上传的图片保存在 public/images 目录中。
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg|max:5048'
]);
$newImageName = uniqid() . '-' . $request->title . '.' . $request->image->extension();
$request->image->move(public_path(('images'), $newImageName));
Post::create([
'title' => $request->input('title'),
'description' => $request->input('description'),
'slug' => SlugService::createSlug(Post::class, 'slug', $request->title),
'image_path' => $newImageName,
'user_id' => auth()->user()->id
]);
return redirect('/blog')
->with('message', 'Dein Beitrag wurde erstellt.');
}
一切正常 - 除了文件名。它的名称应该是 $newImageName,但看起来像 'php51F7.tmp'。此外,扩展名是“.tmp”而不是“.png”或“.jpg”。
非常感谢您的帮助!
PS: 我是Laravel的绝对初学者,请耐心等待
您的代码中有错字
public function store(Request $request)
{
$request->validate([
'title' => 'required',
'description' => 'required',
'image' => 'required|image|mimes:jpg,png,jpeg|max:5048'
]);
$newImageName = uniqid() . '-' . $request->title . '.' . $request->image->extension();
#Notice here, move() accepts two parameteres
$request->image->move(public_path('images'), $newImageName);
Post::create([
'title' => $request->input('title'),
'description' => $request->input('description'),
'slug' => SlugService::createSlug(Post::class, 'slug', $request->title),
'image_path' => $newImageName,
'user_id' => auth()->user()->id
]);
return redirect('/blog')
->with('message', 'Dein Beitrag wurde erstellt.');
}
是额外圆括号的问题
替换
$request->image->move(public_path(('images'), $newImageName));
有
$request->image->move(public_path('images'), $newImageName);