Laravel在上传新文件之前删除文件
根据教程,我可以上传文件,但当我上传另一个文件时,它会将新文件添加到文件夹中。我想先删除该文件,然后用户才能上载新文件 如果我确定文件存在于/uploads/images/nerison123_158139807.PNG中,为什么会返回File Not Exist事件 您可以使用公共路径将图像路径更改为完整路径: 如果文件存在默认路径$image\u路径{ @取消公共路径$image\u路径的链接; }否则{ echo$image\u路径; 回响 返回文件不存在; }Laravel在上传新文件之前删除文件,laravel,Laravel,根据教程,我可以上传文件,但当我上传另一个文件时,它会将新文件添加到文件夹中。我想先删除该文件,然后用户才能上载新文件 如果我确定文件存在于/uploads/images/nerison123_158139807.PNG中,为什么会返回File Not Exist事件 您可以使用公共路径将图像路径更改为完整路径: 如果文件存在默认路径$image\u路径{ @取消公共路径$image\u路径的链接; }否则{ echo$image\u路径; 回响 返回文件不存在; } 如果文件_存在$image
如果文件_存在$image_path{?完整路径是什么?@Erich是的,它是$image_path它是错误的粘贴。@TsaiKoga在我的计算机中它的D:\LaravelImageUpload\public\uploads\images和$image_path是什么?
// Get current user
$user = User::findOrFail(auth()->user()->id);
$image_path = $user->profile_image;
//delete the file first if it exists before uploading the new one
if (file_exists($image_path)) {
@unlink($image_path);
} else {
echo $image_path;
echo "<br>";
return "FILE NOT EXIST";
}
$image = $request->file('image');
$slug = str_slug($request->title);
if (isset($image))
{
$currentDate = Carbon::now()->toDateString();
$imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(1600,1066);
if (!file_exists('storage/uploads/post'))
{
mkdir('storage/uploads/post',0777,true);
}
unlink('storage/uploads/post/'.$post->image);
$image_resize->save('storage/uploads/post/'.$imagename);
}else{
$imagename = $post->image;
}