【问题标题】:Why does uploaded images disapear after deployment?为什么部署后上传的图片会消失?
【发布时间】:2022-01-01 18:36:10
【问题描述】:

我使用 Heroku 部署了一个在 Laravel 8 中制作的项目,它运行良好,但过了一段时间,图像消失了,并且我存储它们的文件没有显示:

并让我再次上传它们,从昨天开始,这个问题发生了多次, 那是我用来存储图像的代码:

 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;
            }

我尝试了什么:

  • 在 vars 中写入了整个文件路径
  • 检查文件是否存在于项目的部署版本中(确实存在):

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    这是 Heroku 上托管的应用程序的预期行为。来自heroku help center

    Heroku 文件系统是短暂的 - 这意味着在 dyno 运行时对文件系统的任何更改只会持续到该 dyno 关闭或重新启动。每个测功机都使用最新部署的文件系统的干净副本启动。

    这基本上意味着每当您的 Dyno 重新启动时(至少每天一次),由于文件系统被重置为应用程序部署时的状态,所有图像都会被删除。 Heroku 不适合持久化数据存储。

    您应该使用 Heroku 提供的外部服务,例如 Amazon S3addon

    【讨论】:

      【解决方案2】:

      Heroku 仅在您的程序需要运行时分配磁盘。如果空闲,磁盘将被释放并分配给其他磁盘。因此,除非文件是您代码的一部分(静态文件),否则它们会消失。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-19
        • 2015-01-29
        • 1970-01-01
        • 2014-09-30
        • 1970-01-01
        • 1970-01-01
        • 2016-07-11
        • 1970-01-01
        相关资源
        最近更新 更多