大网数据广告活动
PHP 语言与运行手册

stream_copy_to_stream · Copies data from one stream to another

· 52CE 我爱测

stream_copy_to_stream — Copies data from one stream to another

stream_copy_to_stream

(PHP 5, PHP 7, PHP 8)

stream_copy_to_stream — Copies data from one stream to another

说明

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

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.

参数

from

The source stream

to

The destination stream

length

Maximum bytes to copy. By default all bytes left are copied.

offset

If 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.

返回值

Returns the total count of bytes copied, 或者在失败时返回 false.

更新日志

版本 说明
8.0.0 length is now nullable.

示例

示例 #1 A stream_copy_to_stream() example

<?php
$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";

?>

注释

注意:

When from is a socket stream in blocking mode, copying stops as soon as no data is received within the stream timeout, even if the end of the stream has not been reached. false may then be returned, even though the data read until then has already been written to to. This timeout defaults to default_socket_timeout and can be changed with stream_set_timeout().

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。