라이브러리

[PHP] stream_copy_to_stream - 한 스트림에서 다른 스트림으로 데이터를 복사합니다.




PHP Stream_copy_to_stream 함수

PHP의 `stream_copy_to_stream` 함수는 두 개의 파일 스트림 사이에 데이터를 복사하는 데 사용됩니다. 이 함수는 `fopen` 함수를 사용하여 열린 파일 스트림을 인수로 받으며, 첫 번째 스트림에서 데이터를 읽어 두 번째 스트림으로 복사합니다.

함수 구조

`stream_copy_to_stream` 함수의 구조는 다음과 같습니다.

#hostingforum.kr
php

stream_copy_to_stream($source, $dest, $length = -1, $offset = 0)



- `$source`: 복사할 데이터를 읽어올 스트림
- `$dest`: 복사한 데이터를 쓸 스트림
- `$length`: 복사할 데이터의 길이 (기본값: -1, 즉 전체 데이터를 복사)
- `$offset`: 복사할 데이터의 시작 위치 (기본값: 0)

예제

다음 예제에서는 `stream_copy_to_stream` 함수를 사용하여 `example.txt` 파일의 내용을 `output.txt` 파일로 복사하는 방법을 보여줍니다.

#hostingforum.kr
php

// example.txt 파일을 열고 읽기 모드로 열기

$source = fopen('example.txt', 'r');

if ($source === false) {

    die('파일 열기 실패');

}



// output.txt 파일을 열고 쓰기 모드로 열기

$dest = fopen('output.txt', 'w');

if ($dest === false) {

    die('파일 열기 실패');

}



// example.txt 파일의 내용을 output.txt 파일로 복사

stream_copy_to_stream($source, $dest);



// 스트림 닫기

fclose($source);

fclose($dest);



echo '파일 복사 성공!';



예제 2: 특정 길이의 데이터 복사

다음 예제에서는 `stream_copy_to_stream` 함수를 사용하여 `example.txt` 파일의 내용 중 10 byte만 `output.txt` 파일로 복사하는 방법을 보여줍니다.

#hostingforum.kr
php

// example.txt 파일을 열고 읽기 모드로 열기

$source = fopen('example.txt', 'r');

if ($source === false) {

    die('파일 열기 실패');

}



// output.txt 파일을 열고 쓰기 모드로 열기

$dest = fopen('output.txt', 'w');

if ($dest === false) {

    die('파일 열기 실패');

}



// example.txt 파일의 내용 중 10 byte만 output.txt 파일로 복사

stream_copy_to_stream($source, $dest, 10);



// 스트림 닫기

fclose($source);

fclose($dest);



echo '파일 복사 성공!';



예제 3: 특정 위치부터 데이터 복사

다음 예제에서는 `stream_copy_to_stream` 함수를 사용하여 `example.txt` 파일의 내용 중 10 byte부터 20 byte까지 `output.txt` 파일로 복사하는 방법을 보여줍니다.

#hostingforum.kr
php

// example.txt 파일을 열고 읽기 모드로 열기

$source = fopen('example.txt', 'r');

if ($source === false) {

    die('파일 열기 실패');

}



// output.txt 파일을 열고 쓰기 모드로 열기

$dest = fopen('output.txt', 'w');

if ($dest === false) {

    die('파일 열기 실패');

}



// example.txt 파일의 내용 중 10 byte부터 20 byte까지 output.txt 파일로 복사

stream_copy_to_stream($source, $dest, 10, 10);



// 스트림 닫기

fclose($source);

fclose($dest);



echo '파일 복사 성공!';



이 예제들은 `stream_copy_to_stream` 함수의 다양한 사용 방법을 보여줍니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 118 페이지

검색

게시물 검색