Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MahmoudMonem's avatar

Problem with updating images on a linked storage directory on server

I have a an update function for my courses that looks like this [code below] .. It is supposed to check if an old picture exists and if it does, it overwrites it with a new one. the function works perfectly fine on localhost but on server, it just deletes the old picture and does not update.

 //update course Image
    public function updateCourseCard(Request $request,$id){


        Validator::make($request->all(),['crs_img'=>"required|file|image|mimes:jpg,png,jpeg|max:5000"])->validate();


        if($request->hasFile("crs_img")){

            $course = Course::find($id);
          $exists = Storage::disk('local')->exists("public/courses/".$course->crs_img);

          //delete old image
          if($exists){
             Storage::disk("local")->delete('public/courses/'.$course->crs_img);

          }

          //upload new image
            $ext = $request->file('crs_img')->getClientOriginalExtension(); //jpg

            $request->crs_img->storeAs("courses/",$course->crs_img);

            $arrayToUpdate = array('crs_img'=>$course->crs_img,
                                   'updated_at' => \Carbon::now());


            DB::table('courses')->where('id',$id)->update($arrayToUpdate);


            return redirect()->route("adminAllCourses")->withSuccess('Course Image Updated!');

        }else{

           $error = "NO Image was Selected";
           return $error;

        }


    }

I made sure that I ran php artisan storage link on my SSH server. I am using Digital ocean .. what is the issue here ? is it a permission issue or what ?

When I ran storage link again i got

The [/public/storage] link already exists.
The links have been created.
0 likes
1 reply
MahmoudMonem's avatar
MahmoudMonem
OP
Best Answer
Level 2

Alright Solved it .. I had public folder in git ignore, hence my public folder and storage folder on server were a miss with so many old files and other useless folders .. also in the update function had to change this

$request->crs_img->storeAs("courses/",$course->crs_img);

to

$request->crs_img->storeAs("public/courses/",$course->crs_img);

because it was actually updloading the new picture, but outside the public folder in storage. hence, I didn't see it.

Now all is fine :)

Please or to participate in this conversation.