exec Execute an external program php函数


(PHP 4, PHP 5)

execExecute an external program


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

exec() executes the given command.



The command that will be executed.


If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as , is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().


If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.


The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

To get the output of the executed command, be sure to set and use the output parameter.


Example #1 An exec() example

// outputs the username that owns the running php/httpd process
// (on a system with the "whoami" executable in the path)
echo exec('whoami');



当用户提供的数据传入此函数,使用 escapeshellarg()escapeshellcmd() 来确保用户欺骗系统从而执行任意命令。


如何程序使用此函数启动,为了能保持在后台运行,此程序必须将输出重定向到文件或其它输出流。 否则会导致 PHP 挂起,直至程序执行结束。

Note: 安全模式 启用时,可仅可用 safe_mode_exec_dir 执行文件。实际上,现在不允许在到可执行的路径中存在 .. 组件。


安全模式 启用时,命令字符串会被 escapeshellcmd() 转换。因此, echo y | echo x 会变成 echo y | echo x


  • system() - Execute an external program and display the output
  • passthru() - Execute an external program and display raw output
  • escapeshellcmd() - Escape shell metacharacters
  • pcntl_exec() - 在当前进程空间执行指定程序
  • backtick operator

exif_imagetype 判断一个图像的类型
exif_read_data 从 JPEG 或 TIFF 文件中读取 EXIF 头信息
exif_tagname Get the header name for an index
exif_thumbnail 取得嵌入在 TIFF 或 JPEG 图像中的缩略图
exit Output a message and terminate the current script
exp 计算 e 的指数
explode 使用一个字符串分割另一个字符串
expm1 返回 exp(number)1,甚至当 number 的值接近零也能计算出准确结果
extension_loaded Find out whether an extension is loaded
extract 从数组中将变量导入到当前的符号表
ezmlm_hash 计算 EZMLM 所需的散列值
fbsql_affected_rows Get number of affected rows in previous FrontBase operation
fbsql_autocommit Enable or disable autocommit
fbsql_blob_size Get the size of a BLOB

Copyright © 2016 phpStudy | 皖ICP备18014864号-4