-1

I am wondering how company like snapchat execute their delete function. If I am about to design a feature like snapchat which the file(images, videos) will be deleted after 24 hours the video have been uploaded by the specific user is to execute sleep in php after 24 hours.

  <?php
    **upload code goes here**
    sleep(24);
    **delete code goes here**/

   ?>

I know this is not the best way to do it. I searched around and found something called cron job which you can execute php code in a scheduled time. But I want to delete the specific file that upload by user after 24 hours not the specific time which when the admin execute the delete. Is there a better way to do this? Thanks

dramasea
  • 3,370
  • 16
  • 49
  • 77
  • 1
    mark them for deletion in the database with a column like `scheduled_deletion` and have a cron job that runs every so often and processes the files marked for deletion. simple enough for you to run with? – r3wt Apr 28 '16 at 13:00
  • Possible duplicate of [Scalable, Delayed PHP Processing](http://stackoverflow.com/questions/3115191/scalable-delayed-php-processing) – r3wt Apr 28 '16 at 13:01
  • Either a server side housekeeping job via something like cron, or you keep it local to the clients only and delete client side after a certain period of time has elapsed. And prevent screen capture if possible. – ManoDestra Apr 28 '16 at 13:47

1 Answers1

2

Definitely don't use sleep because it'll lock your PHP process, preventing it from doing anything else. It won't scale.

In this case you wouldn't create one cron job for each file to delete. Instead you keep a timestamp with the data, then run one cron job every 15 minutes or so and look for timestamps older then 24 hours. Then delete those records/files.

Matt S
  • 14,976
  • 6
  • 57
  • 76
  • so, i save my timestamp into my database, and execute the cron job every 15 minutes? – dramasea Apr 28 '16 at 13:15
  • Yes, exactly. You can run it more or less often depending on how close to 24 hrs you need them to disappear. – Matt S Apr 28 '16 at 13:18