Home PHP Stream stream_copy_to_stream() PHP stream_copy_to_stream() Function 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.
$bytes = stream_copy_to_stream($from, $to); Call What 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 stream_copy_to_stream(resource $from, resource $to, ?int $length = null, int $offset = 0): int|false Parameters Parameter Description from Required. Source stream resource. to Required. Destination stream resource. length Optional. Maximum bytes to copy. null means copy until the end. offset Optional. If greater than 0, seek to this position in $from before copying. 0 or less means start at the current position.
Return Value Return Value Description int|false Number of bytes copied, or false on failure.
PHP Examples 1. Copy Between Memory Streams 2. Copy Only a Length Limit 3. Copy From an Offset Example 1 — Copy Between Memory Streams Write text into one php://memory stream and copy it into another.
<?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 Two memory streams are opened for read and write. Text is written to $from and rewound to the start. stream_copy_to_stream() copies all remaining bytes and returns 13. Reading $to shows Hello streams. Example 2 — Copy Only a Length Limit Pass a length so only the first few bytes are copied.
<?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 The source holds ten letters. length 4 tells PHP to copy at most four bytes. The return value is 4. The destination contains ABCD. Frequently Asked Questions What does stream_copy_to_stream() do in PHP? It copies data from one stream resource to another and returns the number of bytes copied.
How do I copy only part of a stream? Pass a $length limit, and optionally an $offset to choose where copying starts in the source.
Does offset 0 rewind the source? No. Values of 0 or less leave the current position unchanged. Use rewind() or a positive offset.
What does a false return value mean? Copying failed. On some socket streams a timeout can also produce false even after some bytes were written.
Is this better than reading into a string first? For larger data, copying stream-to-stream avoids holding the whole payload in a PHP string at once.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion