PHP exec
exec
(PHP 3, PHP 4, PHP 5)
exec -- Execute an external program
Description
string exec ( string command [, array &output [, int &return_var]] )
exec() executes the given command .
Parameters
command
The command that will be executed.
output
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n , is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec() .
return_var
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.
Return Values
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
To get the output of the executed command, be sure to set and use the output parameter.
PHP exec Examples Quick-and-dirty search function using the Windows NT find command
<form>
Please input a search term and press enter:<br />
<input type="text" name="term" />
</form>
<?php
function nt_search ($term = "") {
// Declare my variables
$max_hits = 0;
$out = $search_results = "";
// Run the NT find command - search all files in the current working directory
exec ("find /C \"$term\" *", $command_output);
// Loop through the lines of output one line at a time
foreach ($command_output as $line) {
/*
Use a regular expression to break the line into a filename and number of results.
This regex accomplishes the work of at least three other function calls.
See the chapter on regular expressions for more information.
*/
ereg ('^-+ (.+): ([0-9]+)$', $line, $capture_buffer);
list (, $filename, $hits) = $capture_buffer;
// Skip over results that had no matches of the term
if ($hits == 0)
continue;
// Convert hits to a ratio of number of results to size of file
$hits /= filesize ($filename);
// Keep track of the largest ratio of hits to file size
$hits > $max_hits
and $max_hits = $hits;
// Create an array of filenames and search results
// Note the use of basename inside the brackets after $search_results
$search_results[basename ($filename)] = $hits;
}
if (! is_array ($search_results))
return "Sorry, no matches for the term '$term' could be found.";
// Sort $search_results by key value, from largest to smallest (aka reverse order)
arsort ($search_results);
// Loop through the sorted results
foreach ($search_results as $filename => $hits) {
// Make the hit ratings relative to the greatest hit rating
$rating = ceil ($hits/$max_hits * 100);
$out .= "\n\n<b>$filename</b>\nSearch Rating of of $rating/100";
}
return ('<b>Your search returned ' . count ($search_results)
. ' search result(s)</b><blockquote>' . $out . '</blockquote>');
}
if (isset ($term))
echo '<pre>' . nt_search ($term) . '</pre>';
?>
|