php:// - 다양한 I/O 스트림 액세스

php:// — 다양한 I/O 스트림 액세스


설명

PHP는 PHP 고유의 입력 및 출력 스트림, 표준 입력, 출력 및 오류 파일 설명자, 메모리 내 및 디스크 지원 임시 파일 스트림, 다른 파일 리소스를 조작할 수 있는 필터에 대한 액세스를 허용하는 다양한 I/O 스트림을 제공합니다. 그들이 읽고 쓰는 것처럼.

php://stdin, php://stdout 및 php://stderr

php://stdin, php://stdoutphp://stderr을 사용하면 PHP 프로세스의 해당 입력 또는 출력 스트림에 직접 액세스할 수 있습니다. 스트림은 중복 파일 설명자를 참조하므로 php://stdin을 열고 나중에 닫으면 설명자 사본만 닫힙니다. STDIN에서 참조하는 실제 스트림은 영향을 받지 않습니다. PHP 5.2.1까지 PHP는 이와 관련하여 버그가 있는 동작을 나타냈습니다. 이러한 래퍼를 사용하여 스트림을 수동으로 여는 대신 STDIN, STDOUTSTDERR 상수를 사용하는 것이 좋습니다.

php://stdin은 읽기 전용이고 php://stdoutphp://stderr은 쓰기 전용입니다.

php://input

php://input은 요청 본문에서 원시 데이터를 읽을 수 있는 읽기 전용 스트림입니다. php://inputenctype="multipart/form-data"와 함께 사용할 수 없습니다.

php://output

php://outputprintecho와 같은 방식으로 출력 버퍼 메커니즘에 쓸 수 있는 쓰기 전용 스트림입니다.

php://fd

php://fd는 주어진 파일 기술자에 대한 직접 접근을 허용합니다. 예를 들어, php://fd/3은 파일 설명자 3을 나타냅니다.

php://memory 및 php://temp

php://memoryphp://temp는 파일과 같은 래퍼에 임시 데이터를 저장할 수 있는 읽기-쓰기 스트림입니다. 둘 사이의 유일한 차이점은 php://memory는 항상 데이터를 메모리에 저장하는 반면 php://temp는 저장된 데이터 양이 미리 정의된 제한(기본값은 2MB)에 도달하면 임시 파일을 사용한다는 것입니다. 이 임시 파일의 위치는 sys_get_temp_dir() 함수와 같은 방식으로 결정됩니다.

php://temp의 메모리 제한은 /maxmemory:NN을 추가하여 제어할 수 있습니다. 여기서 NN은 임시 파일을 사용하기 전에 메모리에 보관할 최대 데이터 양(바이트)입니다.

php://filter

php://filter는 열 때 스트림에 필터를 적용할 수 있도록 설계된 일종의 메타 래퍼입니다. 이것은 readfile(), file()file_get_contents()와 같은 올인원 파일 함수에 유용합니다. 그렇지 않으면 내용을 읽기 전에 스트림에 필터를 적용할 기회가 없습니다.

php://filter 대상은 경로의 일부로 다음 매개변수를 사용합니다. 하나의 경로에 여러 필터 체인을 지정할 수 있습니다. 이러한 매개변수 사용에 대한 자세한 내용은 예를 참조하십시오.


php://filter parameters

Name Description
resource=<stream to be filtered> 이 매개변수는 필수입니다. 필터링하려는 스트림을 지정합니다.
read=<filter list to apply to read chain> 이 매개변수는 선택 사항입니다. 파이프 문자(|)로 구분하여 여기에 하나 이상의 필터 이름을 제공할 수 있습니다.
write=<filter list to apply to write chain> 이 매개변수는 선택 사항입니다. 파이프 문자(|)로 구분하여 여기에 하나 이상의 필터 이름을 제공할 수 있습니다.
<filter list to apply to both chains> read= 또는 write= 접두사가 없는 필터 목록은 읽기 및 쓰기 체인 모두에 적절하게 적용됩니다.

옵션

Wrapper Summary (for php://filter, refer to the summary of the wrapper being filtered)

Attribute Supported
Restricted by allow_url_fopen No
Restricted by allow_url_include php://input, php://stdin, php://memory and php://temp only.
Allows Reading php://stdin, php://input, php://fd, php://memory and php://temp only.
Allows Writing php://stdout, php://stderr, php://output, php://fd, php://memory and php://temp only.
Allows Appending php://stdout, php://stderr, php://output, php://fd, php://memory and php://temp only. (Equivalent to writing)
Allows Simultaneous Reading and Writing php://fd, php://memory and php://temp only.
Supports stat() No. However, php://memory and php://temp support fstat().
Supports unlink() No
Supports rename() No
Supports mkdir() No
Supports rmdir() No
Supports stream_select() php://stdin, php://stdout, php://stderr, php://fd and php://temp only.

Examples

예제 #1 php://temp/maxmemory

이 선택적 매개변수를 사용하면 php://temp가 임시 파일을 사용하기 시작하기 전에 메모리 제한을 설정할 수 있습니다.

                  
<?php
// Set the limit to 5 MB.
$fiveMBs = 5 * 1024 * 1024;
$fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+');

fputs($fp, "hello\n");

// Read what we have written.
rewind($fp);
echo stream_get_contents($fp);
?>
                  
                

예제 #2 php://filter/resource=<stream to be filtered>

이 매개변수는 php://filter 사양 끝에 있어야 하며 필터링하려는 스트림을 가리켜야 합니다.

                  
<?php
/* This is equivalent to simply:
  readfile("http://www.example.com");
  since no filters are actually specified */

readfile("php://filter/resource=http://www.example.com");
?>
                  
                

예제 #3 php://filter/read=<filter list to apply to read chain>

이 매개변수는 파이프 문자 |로 구분된 하나 이상의 필터 이름을 사용합니다.

                  
<?php
/* This will output the contents of
  www.example.com entirely in uppercase */
readfile("php://filter/read=string.toupper/resource=http://www.example.com");

/* This will do the same as above
  but will also ROT13 encode it */
readfile("php://filter/read=string.toupper|string.rot13/resource=http://www.example.com");
?>
                  
                

예제 #4 php://filter/write=<filter list to apply to write chain>

이 매개변수는 파이프 문자 |로 구분된 하나 이상의 필터 이름을 사용합니다.

                  
<?php
/* This will filter the string "Hello World"
  through the rot13 filter, then write to
  example.txt in the current directory */
file_put_contents("php://filter/write=string.rot13/resource=example.txt","Hello World");
?>
                  
                

예제 #5 php://memory and php://temp are not reusable

php://memoryphp://temp는 재사용할 수 없습니다. 즉, 스트림이 닫힌 후에는 다시 참조할 방법이 없습니다.

                  
file_put_contents('php://memory', 'PHP');
echo file_get_contents('php://memory'); // prints nothing