php中的exec()函数怎么用

PHPz
Release: 2020-09-05 10:00:58
Original
12102 people have browsed it

php中的exec()函数的用法:exec()函数用于执行一个外部程序,语法为:【exec(string $command[,array &$output[,int &$return_var ]]);】。

php中的exec()函数怎么用

PHP中提供了几个调用linux命令的函数,exec、system、passthru;下面本篇文章就来介绍一下exec函数。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

exec()函数用来执行一个外部程序。

开启exec()函数:

exec()函数是被禁用的,要使用这个函数必须先开启。首先是 要关掉 安全模式 safe_mode = off。然后在看看 禁用函数列表

disable_functions = proc_open, popen, exec, system, shell_exec, passthru
Copy after login

这里要把 exec 去掉,重启 apache 就OK了。

exec()函数基本用法:

exec ( string $command [, array &$output [, int &$return_var ]] );
Copy after login

$command:表示要执行的命令。

$output:如果提供了 output 参数, 那么会用命令执行的输出填充此数组, 每行输出填充数组中的一个元素。 数组中的数据不包含行尾的空白字符,例如 \n 字符。 请注意,如果数组中已经包含了部分元素,exec() 函数会在数组末尾追加内容。如果你不想在数组末尾进行追加, 请在传入 exec() 函数之前 对数组使用 unset() 函数进行重置。

$return_var:如果同时提供 output 和 return_var 参数, 命令执行后的返回状态会被写入到此变量。

一般来说,我们只要写第一个参数,也就是$command。

因为 exec()函数主要用在执行外部程序,我们这里就以linux系统为例子,做几个demo教程:

<?php
$command = "ls /tmp/test"; //ls是linux下的查目录,文件的命令
exec($command,$array); //执行命令
print_r($array);
?>
Copy after login

返回的结果如下:

[root@krlcgcms01 shell]# php ./exec.php
Array
(
[0] => 1001.log
[1] => 10.log
[2] => 10.tar.gz
[3] => aaa.tar.gz
[4] => mytest
[5] => test1101
[6] => test1102
[7] => weblog_2010_09
)
Copy after login

更多相关知识,请访问 PHP中文网!!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!