【发布时间】:2021-03-02 02:20:59
【问题描述】:
我正在尝试编辑/更新用户表中的图像,当我选择一个新图像并提交表单时,图像名称不会存储在数据库中,而是这个 C:\wamp64\tmp\phpAE1C.tmp保存在图片栏,我不知道为什么,如果你知道为什么,请帮忙。
用户控制器
public function update(Request $request, $id)
{
$request->validate([
'name' => 'required|string|max:225',
'email' => 'required|string|email|max:255|unique:users,email,'.auth()->id(),
'password' => 'sometimes|nullable|string|min:6|confirmed',
]);
$user = auth()->user();
//Handle avatar Upload
if ($request->hasFile('avatar')) {
// Get filename with extention
$filenamewithExt = $request->file('avatar')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenamewithExt, PATHINFO_FILENAME);
// Get just Extention
$extention = $request->file('avatar')->getClientOriginalExtension();
// Filename to store
$filenameToStore = $filename.'_'.time().'.'.$extention;
// Upload Image
$path = $request->file('avatar')->storeAs('public/avatars', $filenameToStore);
}
if ($request->hasFile('avatar')) {
$user->avatar = $filenameToStore;
}
//Handle image Upload
if ($request->hasFile('image')) {
// Get filename with extention
$ImageNameWithExt = $request->file('image')->getClientOriginalName();
// Get just filename
$ImageName = pathinfo($ImageNameWithExt, PATHINFO_FILENAME);
// Get just Extention
$Extentions = $request->file('image')->getClientOriginalExtension();
// Filename to store
$ImageNameToStore = $ImageName.'_'.time().'.'.$Extentions;
// Upload Image
$paths = $request->file('image')->storeAs('public/images', $ImageNameToStore);
}
if ($request->hasFile('image')) {
$user->image = $ImageNameToStore;
}
$user->save();
$input = $request->except('password', 'password_confirmation');
if (!$request->filled('password')) {
$user->fill($input)->save();
return back()->with('success', 'Profile updated successfully!');
}
$user->password = bcrypt($request->password);
$user->fill($input)->save();
return back()->with('success', 'Profile and password updated successfully');
}
edit.blade 中的图片输入字段
<div class="form-group col-md">
<div class="custom-file">
<input type="file" name="image" class="custom-file-input" id="customFile" >
<label class="custom-file-label text-align-left" style="text-align:left;"
for="customFile">Choose file</label>
</div>
</div>
【问题讨论】:
-
图片是否正确保存在您的目录中?
-
我认为您不应该将“public”指定为路径的一部分。
storeAs()方法的第三个参数是磁盘名称。还有$user->password = bcrypt($request->password);是什么???不要这样做。使用Hash门面。 -
@sta no 图像没有保存在目录中
-
默认文件系统使用名为“本地”的默认磁盘,使用 store、stroeAs 等将文件上传到 storage/app 文件夹存储中
-
@sta 好的将使用哈希,但我使用 public 作为存储头像图像的路径的一部分,它工作正常,并且在创建注册用户时我使用 public/images 并且也是工作正常!
标签: laravel image file-upload upload edit