kms69's avatar
Level 1

laravel 9 api : delete file from folder when updating post

I want to delete previously uploaded image after updating via postman but it wont delete the previous image file.

class SocialPostController extends Controller {

/**
 * Store a newly created resource in storage.
 *
 * @param \Illuminate\Http\Request $request
 * @return \Illuminate\Http\Response
 * @throws \Illuminate\Validation\ValidationException
 */
public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required|unique:social_posts,name',
        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:3040',
        'likes_count' => 'required|numeric',
        'comment_count' => 'required|numeric',
        'url' => 'required',
        'status' => 'required|in:active,inactive',
        'sm_page_id' => 'required|numeric',


    ]);
    $name = $request->file('image')->getClientOriginalName();
    $path = $request->file('image')->storeAs('public/images', $name);

    $attribute = array_merge($request->all(), ['user_id' => Auth::id()]);
    $social_post = SocialPost::create($attribute);


    return response(['social_post' => $social_post, 'message' => 'Successful'], 200);
}

/**
 * Display the specified resource.
 *
 * @param int $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    $user = SocialPost::find($id);
    return response(['social_post' => $user, 'message' => 'Successful'], 200);

}

/**
 * Update the specified resource in storage.
 *
 * @param \Illuminate\Http\Request $request
 * @param int $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, SocialPost $social_post)
{

    if ($request->file('image') != '') {
        $path =storage_path('app/public/images/') .$social_post->image;

        if (File::exists($path)) {
            File::delete($path);
        }

        $name = $request->file('image')->getClientOriginalName();
        $path = $request->file('image')->storeAs('public/images', $name);

    } else {
        $name = $social_post->image;
    }

    $attribute = array_merge($request->all(), ['user_id' => Auth::id()]);


    $social_post->update($attribute);

    return response(['social_post' => $social_post, 'message' => 'Successful'], 200);
}

/**
 * Remove the specified resource from storage.
 *
 * @param int $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    DB::table("social_posts")->where('id', $id)->delete();

    return response()->json([
        'success' => true,
        'message' => 'SocialPosts Deleted successfully.'
    ], 200);
}

} ​

0 likes
9 replies
Snapey's avatar

where do you try to delete the previous image?

kms69's avatar
Level 1

@Snapey From the local file. It used to work with the web route but with api via postman it wont work.

Snapey's avatar

@kms69 no, where in your code do you delete the file? Not in the code you listed above....

kms69's avatar
Level 1

@Snapey in the update method

public function update(Request $request, SocialPost $social_post) {

if ($request->file('image') != '') {
    $path =storage_path('app/public/images/') .$social_post->image;

    if (File::exists($path)) {
        File::delete($path);
    }
Snapey's avatar

if you dd the path, does it match the location of the file?

        $path =storage_path('app/public/images/') .$social_post->image;

      dd($path);

        if (File::exists($path)) {
            File::delete($path);
        }

kms69's avatar
Level 1

@Snapey when I use var_dump($path) it will return D:\back-end\storage\app/public/images/C:\Users\KMS\AppData\Local\Temp\sfy6EFC.tmp". it seems to match it

Snapey's avatar

really? have you read what you wrote?

you have C: in the middle of the path?

Seems to me that your database entry is incorrectly containing the path to the temporary file, and then you are prefixing it with the storage path

1 like
Snapey's avatar

@kms69 I don;t think you can, Your database contains invalid entries. You will have to manually clean up the folder since you don't have the path to the stored file.

1 like

Please or to participate in this conversation.