Cron Job Tutorial: Crontab Scheduling Syntax and Script Example
This is a beginner tutorial on using crontabs or cron jobs in your LAMP (Linux/Apache/MySQL and PHP) web hosting account. Crontabs can be used to automatically execute server side commands, such as a PHP script, at periodic intervals. You can specify these periodic intervals, whether you want the script to be executed hourly, daily, weekly, and so on.
Crontabs is very useful when doing website maintenance. You can use this feature for the following purposes:
Deleting all files in a certain folder at regular intervals. If you accept user-uploaded files, then eventually it will clog your web hosting server. If you do not regularly delete these files, they will consume a lot of disk space and can slow down your website.
Deleting files of a specific type at regular intervals. You can also choose to delete files of a specific type, instead of deleting all of the files in the folder. If you have PHP, HTML and MP3 files together inside the folder, you can delete only the MP3 files.
Regularly backing up your MySQL database. This is one of the most important webmaster tasks. By using crontabs or the cron job feature, you can create a PHP script that will back up a selected MySQL database, and have it execute automatically executed at a specific, regular interval (e.g monthly, yearly, etc).
This tutorial will focus on creating a cron PHP script application, then configuring your cron hosting feature to execute these scripts automatically.
Deleting All Files Within a Folder using Cron
Let's have an actual example. Below is the PHP script that will delete all of the files in the folder.
<?php /* First protect this file from direct access. If unauthorized users execute this script using a browser, then they will receive "Direct File Access Prohibited" Errors. */ if ('deleteallfiles.php' == basename($_SERVER['SCRIPT_FILENAME'])) { die ('<h2>Direct File Access Prohibited</h2>'); } else { /*
If this is not a direct file access, execute the script. Next we define which folder to clean in your FTP server. Bear in mind that this is a full server path to the folder. This should start with a forward slash and end with a trailing slash. To determine the full server path, you need to create a PHP file with this script and save it as fullserverpath.php:
<?php echo $_SERVER['SCRIPT_FILENAME']; ?>
Then upload it inside the folder that you need to clean. Execute the script in the browser, e.g http://www.yourdomain.com/folderthistoclean/fullserverpath.php You will see the full path to this folder, such as: /home/www/php-developer.org/testdelete/fullserverpath.php
This means that the full server path to the folder should be: /home/www/php-developer.org/testdelete/, then use this one to define $foldertodelete:
//Delete entire files in the folder , this is using *.*, this means all files. $fileTypes1 = '*.*'; /* Define how old the files in minutes should be when they are deleted. For example, if you set the expiration time to 3 minutes; this means that all files older than 3 minutes will be deleted by this script during cron execution. */
$expire_time1= 3;
//Find all files in the folder using a loop
foreach (glob($foldertodelete . $fileTypes1) as $Filename1) {