PHP stream_copy_to_stream() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_copy_to_stream() copies bytes from one stream to another and returns how many bytes were copied.

You can copy everything remaining, limit the number of bytes, or start at an offset. It is handy for php://memory streams and temporary files when you want an efficient stream-to-stream transfer.

Syntax

Call stream_copy_to_stream() with the values you need. Start with this line.

PHP
$bytes = stream_copy_to_stream($from, $to);
CallWhat it does
stream_copy_to_stream($from, $to)Copies all remaining bytes from $from to $to.
stream_copy_to_stream($from, $to, 5)Copies at most 5 bytes.
stream_copy_to_stream($from, $to, null, 2)Starts copying at offset 2, then copies the rest.

After a successful copy, the source position is where copying stopped. Rewind destination streams before reading them back. These demos use php://memory only, so they stay offline-safe.

Library declarations
PHP
stream_copy_to_stream(resource $from, resource $to, ?int $length = null, int $offset = 0): int|false

Parameters

ParameterDescription
fromRequired. Source stream resource.
toRequired. Destination stream resource.
lengthOptional. Maximum bytes to copy. null means copy until the end.
offsetOptional. If greater than 0, seek to this position in $from before copying. 0 or less means start at the current position.

Return Value

Return ValueDescription
int|falseNumber of bytes copied, or false on failure.

PHP Examples

Example 1 — Copy Between Memory Streams

Write text into one php://memory stream and copy it into another.

PHP
<?php
$from = fopen("php://memory", "w+");
$to = fopen("php://memory", "w+");

fwrite($from, "Hello streams");
rewind($from);

$bytes = stream_copy_to_stream($from, $to);
rewind($to);

echo $bytes, PHP_EOL;
echo stream_get_contents($to), PHP_EOL;
fclose($from);
fclose($to);

How It Works

  1. Two memory streams are opened for read and write.
  2. Text is written to $from and rewound to the start.
  3. stream_copy_to_stream() copies all remaining bytes and returns 13.
  4. Reading $to shows Hello streams.

Example 2 — Copy Only a Length Limit

Pass a length so only the first few bytes are copied.

PHP
<?php
$from = fopen("php://memory", "w+");
$to = fopen("php://memory", "w+");

fwrite($from, "ABCDEFGHIJ");
rewind($from);

$bytes = stream_copy_to_stream($from, $to, 4);
rewind($to);

echo $bytes, PHP_EOL;
echo stream_get_contents($to), PHP_EOL;
fclose($from);
fclose($to);

How It Works

  1. The source holds ten letters.
  2. length 4 tells PHP to copy at most four bytes.
  3. The return value is 4.
  4. The destination contains ABCD.

Example 3 — Copy From an Offset

Use offset to skip the first characters, then copy the remainder into a second memory stream.

PHP
<?php
$from = fopen("php://memory", "w+");
$to = fopen("php://memory", "w+");

fwrite($from, "0123456789");
$bytes = stream_copy_to_stream($from, $to, null, 6);
rewind($to);

echo $bytes, PHP_EOL;
echo stream_get_contents($to), PHP_EOL;
fclose($from);
fclose($to);

How It Works

  1. The source text is ten digits.
  2. offset 6 seeks to digit 6 before copying.
  3. null length means copy from there to the end.
  4. Four bytes are copied: 6789.

Frequently Asked Questions

It copies data from one stream resource to another and returns the number of bytes copied.
Pass a $length limit, and optionally an $offset to choose where copying starts in the source.
No. Values of 0 or less leave the current position unchanged. Use rewind() or a positive offset.
Copying failed. On some socket streams a timeout can also produce false even after some bytes were written.
For larger data, copying stream-to-stream avoids holding the whole payload in a PHP string at once.

Next: stream_filter_append()

stream_filter_append() attaches a named filter to the end of a stream's filter chain so da.

stream_filter_append() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful