操作文件系统
当您进行操作时,使用 exec() 和 PHP 函数(例如 rmdir() 、 unlink() 等)之间的最佳方法是什么文件系统?
What is the best approach between using exec()
and PHP functions (like rmdir()
, unlink()
etc.) when you are manipulating the filesystem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
像 rmdir() 这样的函数适用于每个操作系统,但如果您执行 exec() 命令,您的命令可能只与一种操作系统兼容
Functions like rmdir() work on every OS but if you do an exec() command your command will probably be compatible with just one OS
我建议使用原生 PHP 函数,因为外部命令在许多平台上可能会有所不同。 (以linux/win为例)
I suggest native PHP functions, because external commands may be different in many platforms. (linux/win for example)
独立的 PHP 版本将比启动 shell/调用进程来执行某些操作更快。它们还可以跨不同的操作系统移植,并且即使在网络服务器无法访问标准 shell 命令的情况下(例如由于 chroot / 权限)也可用。
然而,对于特别复杂的操作,使用外部命令/进程可能会更快,并且需要更少的编程工作,例如考虑如何在 php 中实现它:
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:
你为什么要使用 exec ?最好的方法是使用本机 php 函数来操作文件系统。
只有当您需要更复杂的操作时才可以尝试执行命令。但在这种情况下,也许 bash 或 python 脚本可以完成这项工作,并且仅使用 exec 来调用该脚本来完成这项工作。
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.