为什么部署后上传的图片消失了?
Why does uploaded images disapear after deployment?
我用 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 中写入整个文件路径
- 检查文件是否存在于项目的部署版本中(存在):
这是托管在 Heroku 上的应用程序的预期行为。来自 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.
这基本上意味着每当您的 Dyno 重新启动时(至少每天一次),由于文件系统被重置为应用程序部署时的状态,所有图像都会被删除。
Heroku 不适合持久化数据存储。
Heroku 仅在您的程序需要时分配磁盘 运行。如果它空闲,磁盘将被释放并分配给其他人。因此,除非文件是您代码的一部分(静态文件),否则它们将会消失。
我用 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 中写入整个文件路径
- 检查文件是否存在于项目的部署版本中(存在):
这是托管在 Heroku 上的应用程序的预期行为。来自 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.
这基本上意味着每当您的 Dyno 重新启动时(至少每天一次),由于文件系统被重置为应用程序部署时的状态,所有图像都会被删除。 Heroku 不适合持久化数据存储。
Heroku 仅在您的程序需要时分配磁盘 运行。如果它空闲,磁盘将被释放并分配给其他人。因此,除非文件是您代码的一部分(静态文件),否则它们将会消失。