简体   繁体   English

从服务器中删除未链接到项目卡的图像

[英]Removing images from the server that are not linked to the item card

Suppose on the page for adding a product, you can add only one image.假设在添加商品的页面,只能添加一张图片。

Man presses a button to add an image.男人按下按钮添加图像。 Selects the desired image and it is automatically uploaded to the server.选择所需的图像,它会自动上传到服务器。 There is no need to click an additional UPLOAD button.无需单击其他上传按钮。

The first thing I want to draw your attention to is that the product card has not been created yet, and the image has already been loaded.我首先要提醒您注意的是,产品卡片尚未创建,图像已经加载。

Now, suppose the customer has uploaded an image but hasn't created a product card and closed the browser.现在,假设客户已上传图片但尚未创建产品卡片并关闭了浏览器。 Naturally, the uploaded image remains on the server.自然地,上传的图像保留在服务器上。

The image that remains on the server needs to be deleted somehow after a certain time.保留在服务器上的图像需要在一定时间后以某种方式删除。

I also want to draw your attention to the fact that the client can upload the same image several times.我还想提请您注意,客户端可以多次上传同一张图片。

Now the challenge is how to delete images that are not in use?现在的挑战是如何删除不使用的图像?

I tried to do it in different ways.我尝试以不同的方式做到这一点。 I wrote each image to a file, then I tried to process it all and nothing comes out.我将每个图像写入一个文件,然后我尝试处理所有图像,但没有任何结果。

I have been trying to solve this problem for two weeks.两周来我一直在努力解决这个问题。 I've tried a lot of options and it just doesn't work.我尝试了很多选择,但都行不通。

Is there anyone who faced a similar problem and how did you solve it?有没有人遇到过类似的问题,你是如何解决的?

I see two main ways to address this:我看到解决这个问题的两种主要方法:

  1. As pointed out by David Conrad, the usual way is to check for old files that are not referenced by any product and delete them.正如 David Conrad 所指出的,通常的方法是检查未被任何产品引用的旧文件并将其删除。 As you said, you'll need to check the database in order to do that but that shouldn't be too bad.正如您所说,您需要检查数据库才能执行此操作,但这应该不会太糟糕。

  2. You add some file processing to your product creation.您将一些文件处理添加到您的产品创建中。 For example, uploaded files end up into a staging area.例如,上传的文件最终进入暂存区。 When the product is created, you move that file into the "product_images" folder for example.创建产品后,将该文件移动到例如“product_images”文件夹中。 That way, you can have a simple cron job that deletes files older than a set amount of time from the staging area.这样,您就可以拥有一个简单的 cron 作业,从暂存区中删除超过设定时间的文件。

I personally like to have an "upload" table so that I have additional information about the upload like who uploaded it, from which browser/device, etc.我个人喜欢有一个“上传”表,这样我就有了关于上传的额外信息,比如上传者、来自哪个浏览器/设备等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM