4

i wonder how can i schedule and automate tasks in PHP? can i? or is web server features like cron jobs needed.

i am wondering if there is a way i can say delete files after say 3 days when the file are likely outdated or not needed

3 Answers 3

3

PHP natively doesn't support automating tasks, you have to build a solution yourself or search google for available solutions. If you have a frequently visited site/page, you could add a timestamp to the database linking to the file, when visiting your site in a chosen time (e.g. 8 in the morning) the script (e.g. deleteOlderDocuments.php) runs and deletes the files that are older.

Just an idea. Hope it helps.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 If you don't have access to cron jobs this is the way I would go myself, means that you're not hammering the file system or database on every request but means that your task will get done.
2

PHP operates under the request-response model, so it won't be the responsibility of PHP to initiate and perform the scheduled job. Use cron, or make your PHP site to register the cron jobs.

(Note: the script that the job executes can be written in PHP of course)

Comments

1

In most shared hosting environments, a PHP interpreter is started for each page request. This means that for each PHP script in said environment, all that script will know about is the fact that it's handling a request, and the information that request gave it. Technically you could check the current time in PHP and see if a task needs to be performed, but that is relying on a user requesting that script near a given time.

It is better to use cron for such tasks. especially if the tasks you need performed can be slow -- then, every once in a while, around a certain time, a user would have a particularly slow response, because them accessing a script caused the server to do a whole bunch of scheduled stuff.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.