Streams stream_filter_append
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
stream_filter_append — Attach a filter to a stream
설명
stream_filter_append( resource $stream, string $filtername, int $read_write = ?, mixed $params = ? ): resource
stream
에 연결된 필터 목록에 filtername
을 추가합니다.
매개변수
stream
- 대상 스트림.
filtername
- 필터 이름입니다.
read_write
- 기본적으로 stream_filter_append()는 파일이 읽기 위해 열린 경우
read filter chain
에 필터를 첨부합니다(예: 파일 모드:r
및/또는+
). 파일이 쓰기 위해 열린 경우 필터는write filter chain
에도 연결됩니다(예: 파일 모드:w
,a
및/또는+
).STREAM_FILTER_READ
,STREAM_FILTER_WRITE
및/또는STREAM_FILTER_ALL
을read_write
매개변수에 전달하여 이 동작을 재정의할 수도 있습니다. params
- 이 필터는 지정된
params
와 함께 목록 끝에 추가되므로 스트림 작업 중에 마지막으로 호출됩니다. 목록의 시작 부분에 필터를 추가하려면 stream_filter_prepend()를 사용하십시오.
반환 값
성공하면 리소스를 반환하고 실패하면 false
를 반환합니다. 자원은 stream_filter_remove()를 호출하는 동안 이 필터 인스턴스를 참조하는 데 사용할 수 있습니다.
stream
이 리소스가 아니거나 filtername
을 찾을 수 없는 경우 false
가 반환됩니다.
Examples
예제 #1 필터 적용 위치 제어
<?php
/* Open a test file for reading and writing */
$fp = fopen('test.txt', 'w+');
/* Apply the ROT13 filter to the
* write filter chain, but not the
* read filter chain */
stream_filter_append($fp, "string.rot13", STREAM_FILTER_WRITE);
/* Write a simple string to the file
* it will be ROT13 transformed on the
* way out */
fwrite($fp, "This is a test\n");
/* Back up to the beginning of the file */
rewind($fp);
/* Read the contents of the file back out.
* Had the filter been applied to the
* read filter chain as well, we would see
* the text ROT13ed back to its original state */
fpassthru($fp);
fclose($fp);
/* Expected Output
---------------
Guvf vf n grfg
*/
?>
메모
참고: 사용자 정의(사용자) 필터를 사용하는 경우 원하는 사용자 필터를 filtername
에 등록하려면 stream_filter_register()를 먼저 호출해야 합니다.
참고: 스트림 데이터는 리소스(로컬 및 원격 모두)에서 청크로 읽히고 사용되지 않은 데이터는 내부 버퍼에 보관됩니다. 새 필터가 스트림에 추가되면 내부 버퍼의 데이터는 그 시점에 새 필터를 통해 처리됩니다. 이것은 stream_filter_prepend()의 동작과 다릅니다.
참고: 읽기 및 쓰기용 필터가 추가되면 필터의 두 인스턴스가 생성됩니다. 두 필터 리소스를 모두 얻으려면 stream_filter_append()를 STREAM_FILTER_READ
및 STREAM_FILTER_WRITE
로 두 번 호출해야 합니다.
기타
- stream_filter_register() - 사용자 정의 스트림 필터 등록
- stream_filter_prepend() - 스트림에 필터 연결
- stream_get_filters() - 등록된 필터 목록 검색