Home > database >  Why does uploaded images disapear after deployment?
Why does uploaded images disapear after deployment?

Time:01-02

I deployed a project made in Laravel 8 with Heroku , it was working fine , but after some time , the images disapeared, and the file where i store them doesn't show : enter image description here

and made me upload them again , that problem happened multiple times since yesterday , that's the code i used to store the images :

 if($request->hasFile('img')){
        $user_img = $request->file('img');
        $name_gen = hexdec(uniqid()); //tobeSkiped
        $image_ext = strtolower($user_img->getClientOriginalExtension());
        $image_name = $name_gen . '.' . $image_ext;
        $up_location = 'assets/image/utilisateurs/';
        $last_img = $up_location . $image_name ;
        $user_img->move($up_location,$image_name);
        $user->img = $last_img;
        }
        if($request->hasFile('cache')){
            $user_cache = $request->file('cache');
            $name_gen = hexdec(uniqid()); //tobeSkiped
            $image_ext = strtolower($user_cache->getClientOriginalExtension());
            $cache_name = $name_gen . '.' . $image_ext;
            $up_location = 'assets/image/cache/';
            $last_cache = $up_location . $cache_name;
            $user_cache->move($up_location,$cache_name);
            $user->cache = $last_cache;
            }
        if($request->hasFile('logo')){
            $user_logo = $request->file('logo');
            $name_gen = hexdec(uniqid()); //tobeSkiped
            $image_ext = strtolower($user_logo->getClientOriginalExtension());
            $logo_name = $name_gen . '.' . $image_ext;
            $up_location = 'assets/image/logo/';
            $last_logo = $up_location . $logo_name;
            $user_logo->move($up_location,$logo_name);
            $user->logo = $last_logo;
            }

what i tried :

  • wrote the whole file path in the vars
  • checked if the file exists in the deployed version of the project (it did): enter image description here

CodePudding user response:

This is expected behaviour for apps hosted on Heroku. From the heroku help center:

The Heroku filesystem is ephemeral - that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. Each dyno boots with a clean copy of the filesystem from the most recent deploy.

This basically means that whenever your Dyno restarts (at least once daily), all the images get deleted due to the filesystem being reset to the state it had when the app was deployed. Heroku is not suitable for persistent data storage.

You should use an external service such as Amazon S3 or an addon that Heroku provides instead.

CodePudding user response:

Heroku only allocates disks, when your program needs to run. If it's idle, the disks will be freed and allocated to others. So unless the files are part of your code(static ones), they'll disappear.

  • Related