Search code examples
phpsnapchat

How to execute php code after a certain time like snapchat delete?


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


Solution

  • 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.