search
Homephp教程php手册php执行外部命令函数:exec()、passthru()、system()、shell

PHP提供了4种方法执行系统外部命令:exec()、passthru()、system()、shell_exec(),下面分别介绍: 1、exec 原型:string exec ( string $command [, array $output [, int $return_var ]] ) exec执行command命令,但是不会输出全部结果,而是返回结果的最后

PHP提供了4种方法执行系统外部命令:exec()、passthru()、system()、shell_exec(),下面分别介绍:

1、exec

原型:string exec ( string $command [, array &$output [, int &$return_var ]] )

exec执行command命令,但是不会输出全部结果,而是返回结果的最后一行,如果你想得到全部的结果,可以使用第二个参数,让其输出到一个数组,数组的每一个记录代表了输出的每一行,如果输出结果有10行,则数组就有10条记录。所以如果你需要反复输出调用不同系统外部命令的结果,你最好在输出每一条系统外部命令结果时清空这个数组,以防混乱。第三个参数用来取得命令执行的状态码,通常执行成功都是返回0。举例:

<?php exec('ls /home/xyw/test');
?>
没有输出。
<?php exec('ls /home/xyw/test',$arr);
print_r($arr);
?>
输出:
Array
(
    [0] => list.txt
    [1] => list.txt.ln
    [2] => tcpdump中文手册.doc
    [3] => test1
    [4] => 北邮校徽.jpg
    [5] => 浪潮之巅.pdf
)
2、passthru

原型:void passthru ( string $command [, int &$return_var ] )

与exec的区别:passthru直接将结果输出,不返回结果,不用使用echo查看结果。

<?php passthru("ls test");
?>
结果:
list.txt
list.txt.ln
tcpdump中文手册.doc
test1
北邮校徽.jpg
浪潮之巅.pdf
3、system

原型:string system ( string $command [, int &$return_var ] )

与passthru的基本相同,但是system返回结果并且输出。(查看system和pssthru的返回值可以看出)

4、shell_exec

是反撇号 (`) 操作符的变体.

<?php echo `pwd`;
?>
参考:http://hi.baidu.com/cszhi/item/d71d61f22feefe16ce9f32d1

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools