라이브러리

[PHP] Executable::execute - 실행문




PHP에서 Executable::execute에 대한 설명


PHP의 `Executable` 클래스는 PHP 8.1 버전부터 사용할 수 있는 클래스입니다. 이 클래스는 운영 체제에서 실행 가능한 파일(Executable)과 관련된 기능을 제공합니다. `Executable::execute` 메서드는 주어진 파일을 실행하는 데 사용됩니다.

# 사용법


`Executable::execute` 메서드는 다음과 같은 형식으로 사용할 수 있습니다.

#hostingforum.kr
php

Executable::execute($executable, ...$args);



- `$executable`: 실행할 파일의 경로
- `$args`: 실행할 때 사용할 인자들

# 예제


#hostingforum.kr
php

// 예제 1: ls 명령어 실행

Executable::execute('/bin/ls', '-l');



// 예제 2: hello.php 파일 실행

Executable::execute('/path/to/hello.php', '--arg1', '--arg2');



# 예제: PHP로 Hello World 프로그램 만들기


#hostingforum.kr
php

// hello.php 파일 생성

<?php

echo "Hello, World!";

?>



#hostingforum.kr
php

// hello.php 파일 실행

Executable::execute('/path/to/hello.php');



# 예제: PHP로 명령어 실행하기


#hostingforum.kr
php

// 명령어 실행

Executable::execute('/bin/ls', '-l');



# 예제: PHP로 자바 프로그램 실행하기


#hostingforum.kr
php

// 자바 프로그램 실행

Executable::execute('/path/to/java', '-jar', '/path/to/program.jar');



# 예제: PHP로 shell 명령어 실행하기


#hostingforum.kr
php

// shell 명령어 실행

Executable::execute('/bin/bash', '-c', 'echo "Hello, World!"');



# 예제: PHP로 명령어 실행하기 (에러 처리)


#hostingforum.kr
php

try {

    Executable::execute('/path/to/non-existent-program');

} catch (ExecutableExecutableException $e) {

    echo "Error: " . $e->getMessage();

}



# 예제: PHP로 명령어 실행하기 (출력 받기)


#hostingforum.kr
php

$output = Executable::execute('/bin/ls', '-l');

echo $output;



참고


- PHP 8.1 버전 이상에서만 사용할 수 있습니다.
- 운영 체제에서 실행 가능한 파일을 실행할 때 사용합니다.
- 인자로 `$executable`과 `$args`를 전달합니다.
- 에러 처리를 위해 `try-catch` 블록을 사용할 수 있습니다.
- 명령어 실행 결과를 `$output` 변수에 저장할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 569 페이지

검색

게시물 검색