How to restrict user to upload only three images in a month using PHP/MySQL

后端 未结 3 1887
长发绾君心
长发绾君心 2021-01-25 04:40

I am working on PHP. My question is How can restrict a user can upload only three images in a month.

My Mysql Database table --

CREATE TABLE `images`          


        
3条回答
  •  逝去的感伤
    2021-01-25 05:10

    @fabrik solution is the simplest approach, but it has a cave-eat of performance decay when dealing with a lot of images/users.

    If you dont mind to dig a bit more into the solution, I would suggest adding image_upload_credit field to user table. Every time user uploads the image you would reduce the credit by 1.

    Since you already have access to User object at the time of upload (or should have), the complexity of insert is O(1).

    To finish up the solution you need to write a cron (periodically run) task which resets the image_upload_credit on the first day of each month with UPDATE user SET image_upload_credit = 3 WHERE image_upload_credit <> 3;

提交回复
热议问题