PHP中exec与shell_exec函数区别
这两个函数都是执行Linux命令函数,不同的是获取返回结果不一样,exec只能获取最后一行数据,shell_execu则可以获取全部数据。
假如脚本路径下有如下文件
-bash-4.1# ll
总用量 12
-rw-rw-r--. 1 www web 133 7月 16 15:00 a.php
-rw-r--r--. 1 lee web 59 2月 29 17:05 b.php
-rw-r--r--. 1 lee web 81 3月 8 17:00 c.php
先看一下php手册对这二个函数的解释:
exec --- 执行外部程式
语法 : string exec ( string command [, array &output [, int &return_var]] )
说明 :
exec( )执行给予的命令command,不过它并不会输出任何东西,它简单的从命令的结果中传回最后一行,如果你需要去执行一个命令,并且从命令去取得所有资料时,可以使用passthru( )这个函数。
如果有给予参数array,则指定的数组将会被命令所输出的每一行填满,注意 : 如果数组先前已经包含了一些元素的话,exec( )将会把它附加在数组的后面,如果你不想要此函数附加元素的话,你可以在传递此数组给exec( )之前呼叫unset( )。
exec例子
代码如下 | 复制代码 |
/** '; print_r($data); echo ' Copy after login Copy after login ?>
|
执行结果
-rw-r--r--. 1 lee web 81 Mar 8 17:00 c.phpshell_exec例子
shell_exec()
shell_exec() 命令行实际上仅是反撇号 (`) 操作符的变体。如果您编写过 shell 或 Perl 脚本,您就知道可以在反撇号操作符内部捕捉其他命令的输出。例如,清单 1 显示了如何使用反撇号在当前目录中获取每个文本(.txt)的单词计数。
在 shell_exec()
中运行相同的命令
代码如下 | 复制代码 |
echo $results ?> |
在图 1 中可以看到,获得的结果与从 shell 脚本得到的一样
exec与shell_exec的区别
代码如下 | 复制代码 |
/** * * 琼台博客 */ $data = shell_exec('/bin/ls -l'); echo ' '; print_r($data); echo ' Copy after login Copy after login ?> |
执行结果
total 12
-rw-rw-r--. 1 www web 139 Jul 16 2012 a.php
-rw-r--r--. 1 lee web 59 Feb 29 17:05 b.php
-rw-r--r--. 1 lee web 81 Mar 8 17:00 c.php所以平时使用exec函数的童鞋注意了,如果你需要获取全部返回信息,就应该使用shell_exec函数,当然,如果命令执行结果只有一行返回信息,那么使用哪个都无所谓

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

To restart the Apache server, follow these steps: Linux/macOS: Run sudo systemctl restart apache2. Windows: Run net stop Apache2.4 and then net start Apache2.4. Run netstat -a | findstr 80 to check the server status.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
