tags:

views:

64

answers:

3

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

+1  A: 

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)

yclian
+1  A: 

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.

Flakron Bytyqi
+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.
MarkRobinson
+1  A: 

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.

Carson Myers