Manipulating the filesystem
What is the best approach between using exec()
and PHP fun开发者_StackOverflow社区ctions (like rmdir()
, unlink()
etc.) when you are manipulating the filesystem?
Functions like rmdir() work on every OS but if you do an exec() command your command will probably be compatible with just one OS
I suggest native PHP functions, because external commands may be different in many platforms. (linux/win for example)
The PHP versions in isolation will be faster than starting up a shell/invoking a process to carry out some operation. They'll also be portable across across different operating systems and will be available even where the webserver does not have access to the standard shell commands (e.g. due to chroot / permissions).
However for particularly complex operations, then using an external command/process may be much faster, and require significantly less programming effort e.g. consider how you would implement this in php:
find /some/path -iname \*.files -exec grep '*.tgz' | \
xargs tar -tvzf | grep targetfile
why would you use exec ? The best is to use the native php functions for manipulating the filesystem.
Only if you need more complicated operations you can try executing commands. But in this case, maybe a bash or python script could do the job and use exec only to call that script to do it.
Using the functions has the advantages that your program remains self-contained, ie it won't have dependencies on external and OS dependent utilities.
精彩评论