masumluf's avatar

Failed to Delete Image from Public Folder in Laravel.

Hello I've tried a lot of way such as

public function destroy(Brightstudent $brightstudent)
    {

        $file=$brightstudent->bright_student_photo;
        $image_path = asset('/images').'/'.$file;

        dd($image_path); // "http://127.0.0.1:8000/images/9340261575748705.edit.PNG"


        if (Storage::delete('/images' . $file)){
            return "file deleted";
        }else{
            return "not deleted";
        }

    } 

and below's code too..

    if (File::delete('/images' . $file)){
            return "file deleted";
        }else{
            return "not deleted";
        }

    if (Storage::delete($image_path)){
            return "file deleted";
        }else{
            return "not deleted";
        }

  if (File::delete($image_path)){
            return "file deleted";
        }else{
            return "not deleted";
        }

Storage::delete($image_path)

Storage::storage('public')->delete($image_path)
Storage::storage('public')->delete($file)

Yes I've added File and Storage Helper class above. Full is code as below

<?php

namespace App\Http\Controllers;

use App\Model\Brightstudent;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;

class BrightStudentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $total_student = Brightstudent::all()->count();

        $brightstudents = Brightstudent::paginate(5);

        return view('Admin.BrightStudentlist', compact('brightstudents', 'total_student'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
       return view('Admin.Brightstudent');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $file=$request->pic;


        $profileImage = str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT).time() . '.' . $file->getClientOriginalName();

        $destinationPath = public_path('/images/'); // upload path
        $file->move($destinationPath, $profileImage);
        $data= ["bright_student_name"=>$request->bright_student_name,"bright_student_details"=>$request->bright_student_details,"bright_student_photo"=>$profileImage,"created_at"=>Carbon::now(),"updated_at"=>Carbon::now()];
        Brightstudent::create($data);
    }


    /**
     * Display the specified resource.
     *
     * @param  \App\Model\Brightstudent  $brightStudent
     * @return \Illuminate\Http\Response
     */
    public function show(Brightstudent $brightStudent)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Model\Brightstudent  $brightStudent
     * @return \Illuminate\Http\Response
     */
    public function edit(Brightstudent $brightstudent)
    {

        return view('Admin.EditBrightStudent', compact('brightstudent'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Model\Brightstudent  $brightStudent
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Brightstudent $brightstudent)
    {
       $brightstudent->update($request->all());
        return response()->json('Updated', 200);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Model\Brightstudent  $brightStudent
     * @return \Illuminate\Http\Response
     */
    public function destroy(Brightstudent $brightstudent)
    {

        $file=$brightstudent->bright_student_photo;
        $image_path = asset('/images').'/'.$file;

        dd($image_path);


        if (Storage::delete('/images' . $file)){
            return "file deleted";
        }else{
            return "not deleted";
        }

    }
}

I am useing Laravel 6. Any suggestion ?

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

Deleting an asset requires the file system path not the URL

and Storage uses specific folders. instead, unlink the file (regular php)

    public function destroy(Brightstudent $brightstudent)
    {
        $image_path = public_path('images/' . $brightstudent->bright_student_photo);

        if (unlink($image_path)){
            return "file deleted";
        }else{
            return "not deleted";
        }

    }

or create a Storage disk that uses public/images

Please or to participate in this conversation.