Current Location: Home> Function Categories> stream_copy_to_stream

stream_copy_to_stream

Copy data from one stream to another
Name:stream_copy_to_stream
Category:Stream
Programming Language:php
One-line Description:Copy data from one stream to another

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:

  • $source: The handle to the source stream, which can be an open file handle or a URL.
  • $dest: The handle to the target stream, which can be an open file handle or a URL.
  • $maxlength (optional): The maximum number of bytes to copy, default is -1, indicating the entire content of the copy source stream.
  • $offset (optional): The starting position of the source stream, default to 0, indicating that the copy starts from the beginning of the source stream.

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.

Similar Functions
Popular Articles