我的公共文件夹 profile_images 中有文件夹,我的图像存储在其中我想在上传新图像时从此文件夹中删除旧图像。
这是我的整个控制器代码:
如何在上传时删除旧文件。
您的帮助将不胜感激。
public function uploadImage(Request $request)
{
$user_id = $request->input('user_id');
$image = $request->input('image');
$r = [
'user_id' => $user_id,
];
$validator = Validator::make($r, [
'user_id' => 'required|exists:users,id',
]);
if($validator->fails()) {
return response(['status' => false, 'message' => 'Validation Errors', 'errors' => $validator->errors()->all()], 500);
}
if ($validator->fails()) {
return response([
'status' => false,
'message' => __('messages.validation_errors'),
'errors' => $validator->errors()->all()], 200);
}
try {
$path = public_path('profile_images');
@mkdir($path, '0777', true);
$image = base64_decode($image);
$imageName = str_random(10).'.'.'png';
Storage::disk('profile-image')->put($imageName, $image);
$path = asset('public/profile_images/' . $imageName);
$this->userBasicInfo->where('user_id', $user_id)->update(['profile_pic' => $path]);
return response(['status' => true, 'message' => 'Image Uploaded successfully', 'data' => ['profile_image' => $path]], 200);
} catch (\Exception $ex) {
return response(['status' => false, 'message' => $ex->getMessage()], 500);
}
}
凤凰求蛊
侃侃尔雅