图像更新并删除laravel上的旧图像

时间:2019-09-03 09:25:59

标签: php laravel-5.8

尝试在我的更新控制器中实现更新文章,这似乎可行,但是问题是当我只想更新帖子而没有上传旧的总是被删除的图像时,是不应该的。

这是我的商店功能

public function store(Post $post)
{
        $post->update($this->validateRequest());

        $this->storeImage($post);


        return redirect('post/'.$post->id)->with('success', 'New ariticle has been posted');
    }

} 

这是我的验证

private function validateRequest()
    {
        return request()->validate([
            'title'=> 'required',
            'content' => 'required',
            'image' => 'sometimes|image|max:5000',

        ]);
    }

这是我的更新功能

public function update(Post $post)
{
        File::delete(public_path('storage/'.$post->image)); 
        $post->update($this->validateRequest());

        $this->storeImage($post);


        return redirect('post/'.$post->id)->with('success', 'This post has 
        been Edited');
    }

} 

我试图将File::delete添加到我的storeImage函数中,并将其从我的更新函数中删除,它解决了该问题,但是旧图像并未从目录中删除

private function storeImage($post)
{

  if (request()->has('image')){
  File::delete(public_path('storage/'.$post->image))
  $post->update([
       'image' => request()->image->store('uploads', 'public'),
  ]);

  $image = Image::make(public_path('storage/'.$post->image))->fit(750, 300);
  $image->save();
        }
    }

好吧,因为我在控制器中使用了模型绑定,所以我不必找到ID对吗? 所以我基本上按照Akhtar munir的建议更改了更新功能,结果是这样的。图像更新工作,更新旧图像时也会删除它。但是我发现了另一个问题,问题是当我编辑文章和标题时,它没有像更新时一样发生变化,希望您能看看这是正确的吗?

public function update(Post $post){
    $this->validateRequest();
    if(request()->hasFile('image') && request('image') != ''){
            $imagePath = public_path('storage/'.$post->image);
            if(File::exists($imagePath)){
                unlink($imagePath);
            }
            $image = request()->file('image')->store('uploads', 'public');
            $post->update([
                'title' => request()->title,
                'content' => request()->content,
                'image' => $image,
            ]);
        }
}

2 个答案:

答案 0 :(得分:1)

尝试这个

private function storeImage($post)
{

    if (request()->hasFile('image')){
        $image_path = "/storage/".'prev_img_name';  // prev image path
        if(File::exists($image_path)) {
            File::delete($image_path);
        }
        $post->update([
           'image' => request()->image->store('uploads', 'public'),
        ]);

        $image = Image::make(public_path('storage/'.$post->image))->fit(750, 300);
        $image->save();
    }
}

答案 1 :(得分:0)

这是我用一种方法完成的工作。可能会对您有帮助。

public function update(Request $request, $id)
{
    if (UserDocument::where('id',$id)->exists()) {

        $this->validateUserDocument($request);

        if ($request->hasFile('doc_file') && $request->doc_file != '') {

            $doc = UserDocument::where('id',$id)->first();
            // dd($doc);
            $file_path = storage_path().'/app/'.$doc['doc_file'];
            //You can also check existance of the file in storage.
            if(Storage::exists($file_path)) {
               unlink($file_path); //delete from storage
               // Storage::delete($file_path); //Or you can do it as well
            }

            $file = $request->file('doc_file')->store('documents'); //new file path

            $doc->update([
                'title' => $request->title,
                'doc_file' => $file //new file path updated
            ]);

            session()->flash('success','Document updated successfully!');
            return redirect()->route('userdocs');
        }

        session()->flash('error','Empty file can not be updated!');
        return redirect()->back();
    }
    session()->flash('error','Record not found!');
    return redirect()->back();
}

在此代码中,我只是想向您说明我已将图像路径存储在数据库中,首先我已检索该路径,并使用该路径在本地存储中找到了图像,请先删除它,然后再更新它与新的。但是请确保在两种情况下都通过插入和更新将图像路径存储在数据库中。

因此,最后您还可以像这样优化代码,无论图像和所有数据还是仅标题和内容,它都将按照您期望的方式工作。

public function update(Post $post){
    $this->validateRequest();
    $data = [
        'title' => request()->title,
        'content' => request()->content
    ];
    if (request()->hasFile('image') && request('image') != '') {
        $imagePath = public_path('storage/'.$post->image);
        if(File::exists($imagePath)){
            unlink($imagePath);
        }
        $image = request()->file('image')->store('uploads', 'public');
        $data['image'] = $image;
        $post->update($data);
    }
    $post->update($data);
}