Using *NIX tools in your script
This will show you how to integrate common and useful *NIX tools into your php scripts.
Once you know how to do this you can do anything you like with a script.
I use this technique a lot for database/file system backups, sending emails or handle cron jobs in general.

There are basically 3 functions to .execute programs from within a php script:

  • exec()
  • system()
  • shell_exec() or the backtick ` operator (these two are identical)
Attention: All these functions are disabled or at least affected when safe_mode is on.
Make sure that it is not before you are getting desperate here.

When you only want to execute a program but don't expect any output you will be fine with exec():

exec('rm -r /tmp/*')
deletes the contents of your /tmp directory and because of the -f flag won't return anything anyway.
If you expect an output from the command you are better off using exec() or shell_exec. dirsize.php /path/to/dir # 1: <?php
# 2:
if($argc || !@is_dir($argv[1]))   {
# 3:   die(
"Please give a directory path\n");
# 4:   }
# 5:
$dir $argv[1];
# 6:
$command 'du --max-depth=2 ' $dir ' | sort -r -n';
# 7:
$res shell_exec($command);
# 8: echo 
"
# 9: <pre>
#10:
$res
#11: </pre>
#12: "
;
#13:
?>
will return the size of the directory /path/to/dir and up to 2 levels of sub directories reverse sorted by size.
Lines 2-4 are just a check to see if an argument has been passed to the script and if the given path exists.
This example is identical to dirsize2.php /path/to/dir # 1: <?php
# 2:
if($argc || !@is_dir($argv[1]))   {
# 3:   die(
"Please give a directory path\n");
# 4:   }
# 5:
$dir $argv[1];
# 6:
$command 'du --max-depth=2 ' $dir ' | sort -r -n';
# 7:
$res = `$command`;
# 8: echo 
"
# 9: <pre>
#10:
$res
#11: </pre>
#12: "
;
#13:
?>
However, I personally find the shell_exec() version more readable, if you don't watch carefully you might mistake the backticks for single quotes...

If you now compare this example with the other functions you will easily see the difference: dirsize3.php /path/to/dir # 1: <?php
# 2:
if($argc || !@is_dir($argv[1]))   {
# 3:   die(
"Please give a directory path\n");
# 4:   }
# 5:
$dir $argv[1];
# 6:
$command 'du --max-depth=2 ' $dir ' | sort -r -n';
# 7:
$res shell_exec($command);
# 8: echo 
"Output with shell_exec()
# 9: -----------------------------------------------------
#10:
$res
#11: -----------------------------------------------------
#12: "
;
#13:
#14:
$res2 system($command$retval);
#15: echo 
"Output with system()
#16:
$res2
#17: -----------------------------------------------------
#18: returned 
$retval
#19: -----------------------------------------------------
#20: -----------------------------------------------------
#21: "
;
#22:
#23:
$res3 exec($command);
#24: echo 
"Output with exec()
#25: -----------------------------------------------------
#26:
$res3
#27: -----------------------------------------------------
#28: "
;
#29:
#30:
$res3 exec($command$output);
#31: echo 
"Output with exec() and return array
#32: -----------------------------------------------------
#33: "
;
#34: foreach(
$output as $line) {
#35:   echo 
"$line \n";
#36:   }
#37: echo 
"-----------------------------------------------------
#38: "
;
#39:
?>
I will show you the output for the pixhound lang/ directory on this machine: # 0: <?
# 1: Output with shell_exec
()
# 2: -----------------------------------------------------
# 3:
24      ../pixhound-0.1/lang
# 4: 10      
../pixhound-0.1/lang/German
# 5: 10      
../pixhound-0.1/lang/English
# 7:
-----------------------------------------------------
# 8:
24      ../pixhound-0.1/lang
# 9: 10      
../pixhound-0.1/lang/German
#10: 10      
../pixhound-0.1/lang/English
#11: Output with system
()
#12:
10      ../pixhound-0.1/lang/English
#13:
-----------------------------------------------------
#14:
returned 0
#15:
-----------------------------------------------------
#16: -----------------------------------------------------
#17:
Output with exec()
#18: -----------------------------------------------------
#19:
10      ../pixhound-0.1/lang/English
#20:
-----------------------------------------------------
#21:
Output with exec() and return array
#22: -----------------------------------------------------
#23:
24      ../pixhound-0.1/lang
#24: 10      
../pixhound-0.1/lang/German
#25: 10      
../pixhound-0.1/lang/English
#26:
-----------------------------------------------------
While shell_exec immediately does what you'd expect system() does not.
This function only saves the last line into a variable and just displays the rest. Not what we want here.

exec() is somewhat more tricky. If you don't give a second parameter to this function it will send back the last line of the result to you, see above.
But you can give an array name as the second paramater and the output will be saved in to this array line by line!
This is handy when you want to further work on the lines that are returned.

Both system() and exec() give you the option to define an additional parameter that will store the return value of the program you called. While normally you don't need it it can be useful if you want to check if an otherwise voiceless command failed or succeeded.
To know which return value means what you'll have to look up the man pages (like man du in our example).

Previous:Passing variables to your script Start Next:Sample application: SimpleBackup.php

My Sites