javascript - How to get web server to delete a file on the system after a period of time OR when they close the tab
Get the solution ↓↓↓People who use my website can grab videos from this certain school website, by submitting the link in a form. My web server then downloads it, and stores it in a folder so the person can download it.
I'm trying to figure out how I can get it to automatically delete the video file from the folder after a certain period of time (let's say 5 minutes), or if it detects that the user has closed the tab.
I don't really know where to start with this sort of thing, so can anyone please give me some guidance on how to do this? Thanks in advance! :)
Answer
Solution:
You can use Cron Jobs to fulfill your task.
What is a Cron Job?
It is a Linux command that is used to schedule tasks from time to time. In this case, you can schedule your file deleting tasks there.
How to create the function (overview)?
In your case, you may need to maintain a DB Table that keeps the date that the user requested to download your file and any sort of other data like IDs. Then, you can create a function to loop through each table row and find files that need to be deleted. This can be done with anif
clause. (if file date is greater than the current date). Then, delete the files as you need.
How to schedule cron?
This depends on the server Operating system and environment you use. I assume you use Linux. Then you have to run the below commands:
- First visit crontab configuration file in your terminal :
crontab –e
- Then add your cron job. Let's say you need to run your function every 5 minutes.
5 * * * * /usr/bin/php -f /usr/local/bin/delete.php &> /dev/null
Here, the first5 * * * *
means that the cron job should run every 5 minutes. The second argument defines the PHP interpreter. In 3rd argument, it defines the file.
For more information on how to define cron jobs : https://www.digitalocean.com/community/tutorials/how-to-use-cron-to-automate-tasks-on-a-vps
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: composer detected issues in your platform: your composer dependencies require a php version ">= 8.0.2".
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.