PHP shell_exec() 및 exec() 함수

John Wachira 2023년6월20일
  1. PHP shell_exec() 함수
  2. PHP exec() 함수
PHP shell_exec() 및 exec() 함수

이 기사에서는 PHP shell_exec()exec() 기능에 대해 설명합니다. 두 기능을 차별화하면서 각 기능이 어떻게 수행되고 실제 사용되는지 살펴보겠습니다.

PHP shell_exec() 함수

shell_exec() 함수를 사용하여 셸에서 명령을 실행하고 출력을 문자열로 반환합니다. shell_exec는 백틱 연산자 *nix의 별칭입니다.

통사론:

string shell_exec( $cmd )

이 함수는 $cmd라는 하나의 매개변수를 허용합니다. 실행할 명령이 포함되어 있습니다.

오류가 있는 경우 함수는 NULL을 반환합니다.

PHP가 안전 모드에 있을 때는 함수가 실행되지 않는다는 점에 유의해야 합니다.

예:

<?php
// Use ls command
$output = shell_exec('ls');

//List all files and directories
echo "<pre>$output</pre>";
?>

출력:

Insert.php
index.html
delft.php

PHP exec() 함수

exec() 함수는 외부 프로그램을 실행하고 출력의 마지막 줄을 반환합니다. 명령이 실패하면 NULL을 반환합니다.

통사론:

string exec( $command, $output, $return_var )

매개변수:

  1. $command에는 실행할 명령이 포함되어 있습니다.
  2. $output은 채울 배열을 지정합니다.
  3. $return_varoutput 인수와 함께 제공됩니다.

예:

<?php
echo exec('iamexecfunction');
?>

출력:

delft.php
작가: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn