stream_copy_to_stream()

stream_copy_to_stream() — Copies data from one stream to another.

Built-in function of PHP 8.5. The manual lists it as available in PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Makes a copy of up to length bytes of data from from to to, starting at the current position in from, or at offset if it is greater than 0. If length is null, all remaining content in from will be copied.

Signature

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

Parameters

ParameterTypeWhat it takes
$fromresourceThe source stream
$toresourceThe destination stream
$length?intMaximum bytes to copy. By default all bytes left are copied.
$offsetintIf greater than 0, the position in from, counted from the start of the stream, at which to start copying: from is first moved to that position, as with fseek() and SEEK_SET. If 0 (the default) or negative, no seek is performed and copying starts at the current position in from.

Return value

Returns the total count of bytes copied, or false on failure.

Example

A runnable snippet:

$src = fopen('http://www.example.com', 'r'); $dest1 = fopen('first1k.txt', 'w'); $dest2 = fopen('remainder.txt', 'w'); echo stream_copy_to_stream($src, $dest1, 1024) . " bytes copied to first1k.txt\n"; echo stream_copy_to_stream($src, $dest2) . " bytes copied to remainder.txt\n";

Changelog

VersionChange
8.0.0length is now nullable.

See also

copy()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page stream_copy_to_stream() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.