I’m a Linux SysAdmin, and always work in Bash. I am very experienced administrating PHP, but fairly new to writing PHP scripts.
What reasons would I want to get output from a system command, rather than PHP internal functions. I would assume you have portability concerns, although that is not a problem in my case.
For example,
I could get a directory listing with ls, and an exec style function.
or do something with opendir(‘.’), and a loop.
What are the performance considerations?
Here is what I have now,
<?php
// Directory Open
$myDirectory = opendir('/media/ARCHIVE/Documents/conf/web_confs');
// Array of Elements
while($entryName = readdir($myDirectory)) {
$dirArray[] = $entryName;
}
// Directory Close
closedir($myDirectory);
// Sort Array
sort($dirArray);
?>
Is there a more effeciant way to replace that loop? Performance does matter in my case, these will be made into snippet like things.
this is system related code, that will not be taking any type of input. Not even from a the URL. In addition the environment is of large scale, and all the systems are performance tuned and often profiled.
So doing it the best way possible once is what i’m looking for.
I really would appreciate any input, as my experience is somewhat limited.
For this specific case there is scandir (http://www.php.net/manual/en/function.scandir.php), which I would use.
It’s probably faster to use PHP’s built-in functions to do these things as I guess doing an exec() has some overhead. Honestly I don’t think you’ll have a performance problem unless you are getting directory listings thousands of times in a single script. Also, benchmark it if you really care, but I think you’ll be doing micro-optimizations that don’t make a significant improvement. (Premature optimization is the root of all evil!)
You’ll have security issues if you take user input and don’t sanitize it. I would be extremely careful at sanitizing input that’s then passed to exec().