Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does uploaded images disapear after deployment?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading