PHP as a shell scripting language

For the most part when I've needed something done from the command line I make use of a BASH shell script. Sometimes that is impractical. For example I was asked by an employer to create a script to be run regularly to delete image entries from a database as well as the physical files themselves that were older then a specified amount of time.

Using a shell script for this was certainly possible but would have required much more string manipulation to get the details from the MySQL database I was looking for. PHP would be far easier specifically for the image upload dates, and the location of the images themselves. Using PHP I could create a query, loop through the results, grab the date uploaded and the actual location of the file.

The script created also maintains a list and emails the list of images deleted to the user the script is being run for. Using cron I have scheduled the script to run once a month. Using PHP rather than BASH to execute this has saved me a good bit of time and is just as reliable. Definitely something I recommend checking out.

Comments