Function name: stream_copy_to_stream()
Function Description: stream_copy_to_stream() function copies data from one stream to another. It continuously reads the contents of the source stream and writes it to the target stream until the contents of the source stream are copied to the target stream or reach the specified maximum number of bytes.
Applicable version: PHP 4.3.0 and above
Syntax: int stream_copy_to_stream ( resource $source , resource $dest [, int $maxlength = -1 [, int $offset = 0 ]] )
parameter:
Return value: Returns the actual number of bytes copied when successful, and returns false when failure.
Example:
// 打开源文件和目标文件的流$sourceStream = fopen('source.txt', 'r'); $destStream = fopen('destination.txt', 'w'); // 将源流的内容拷贝到目标流$bytesCopied = stream_copy_to_stream($sourceStream, $destStream); // 关闭流fclose($sourceStream); fclose($destStream); if ($bytesCopied !== false) { echo "成功拷贝了{$bytesCopied} 字节的数据。"; } else { echo "拷贝失败。"; }
In the example above, we open a stream of a source file and a target file, and then use the stream_copy_to_stream() function to copy the contents of the source stream to the target stream. Finally, we close the stream and judge whether the copy is successful based on the return value. If the copy is successful, the number of bytes of the copied is output, otherwise the error message is output.