src/AbstractStream.php
public function copy($source)
    {
        if (! $this->resource) {
            throw new RuntimeException('No resource available; cannot copy.');
        }
        $resource = null;
        if ($source instanceof self) {
            $resource = $source->getResource();
        } elseif (is_resource($source)) {
            $resource = $source;
        } elseif (is_string($source)) {
            set_error_handler(function () {
                throw new Exception('Failed to open stream.');
            });
            try {
                $resource = fopen($source, 'rb');
            } catch (Exception $ex) {
                restore_error_handler();
                throw new InvalidArgumentException(sprintf(
                    'Invalid source path "%s" specified.',
                    $source
                ));
            }
            restore_error_handler();
        } else {
            throw new InvalidArgumentException(sprintf(
                'Invalid source "%s" provided.',
                is_object($source) ? get_class($source) : gettype($source)
            ));
        }
        $metadata = stream_get_meta_data($resource);
        $mode     = $metadata['mode'];
        if (! (strstr($mode, 'r') || strstr($mode, '+'))) {
            throw new InvalidArgumentException(
                'The received source is not readable.'
            );
        }
        $seekable = $metadata['seekable'];
        if ($seekable) {
            rewind($resource);
        }
        stream_copy_to_stream($resource, $this->resource);
        rewind($this->resource);
        if (is_string($source)) {
            fclose($resource);
        }
    }
public function copy($source)
    {
        if (! $this->resource) {
            throw new RuntimeException('No resource available; cannot copy.');
        }
        $resource = null;
        if ($source instanceof self) {
            $resource = $source->getResource();
        } elseif (is_resource($source)) {
            $resource = $source;
        } elseif (is_string($source)) {
            set_error_handler(function () {
                throw new Exception('Failed to open stream.');
            });
            try {
                $resource = fopen($source, 'rb');
            } catch (Exception $ex) {
                restore_error_handler();
                throw new InvalidArgumentException(sprintf(
                    'Invalid source path "%s" specified.',
                    $source
                ));
            }
            restore_error_handler();
        } else {
            throw new InvalidArgumentException(sprintf(
                'Invalid source "%s" provided.',
                is_object($source) ? get_class($source) : gettype($source)
            ));
        }
        $metadata = stream_get_meta_data($resource);
        $mode     = $metadata['mode'];
        if (! (strstr($mode, 'r') || strstr($mode, '+'))) {
            throw new InvalidArgumentException(
                'The received source is not readable.'
            );
        }
        $seekable = $metadata['seekable'];
        if ($seekable) {
            rewind($resource);
        }
        stream_copy_to_stream($resource, $this->resource);
        rewind($this->resource);
        if (is_string($source)) {
            fclose($resource);
        }
    }
public function copy ( $ source ) { if ( ! $ this -> resource ) { throw new RuntimeException ( 'No resource available; cannot copy.' ) ; } $ resource = null ; if ( $ source instanceof self ) { $ resource = $ source -> getResource ( ) ; } elseif ( is_resource ( $ source ) ) { $ resource = $ source ; } elseif ( is_string ( $ source ) ) { set_error_handler ( function ( ) { throw new Exception ( 'Failed to open stream.' ) ; } ) ; try { $ resource = fopen ( $ source , 'rb' ) ; } catch ( Exception $ ex ) { restore_error_handler ( ) ; throw new InvalidArgumentException ( sprintf ( 'Invalid source path "%s" specified.' , $ source ) ) ; } restore_error_handler ( ) ; } else { throw new InvalidArgumentException ( sprintf ( 'Invalid source "%s" provided.' , is_object ( $ source ) ? get_class ( $ source ) : gettype ( $ source ) ) ) ; } $ metadata = stream_get_meta_data ( $ resource ) ; $ mode = $ metadata [ 'mode' ] ; if ( ! ( strstr ( $ mode , 'r' ) || strstr ( $ mode , '+' ) ) ) { throw new InvalidArgumentException ( 'The received source is not readable.' ) ; } $ seekable = $ metadata [ 'seekable' ] ; if ( $ seekable ) { rewind ( $ resource ) ; } stream_copy_to_stream ( $ resource , $ this -> resource ) ; rewind ( $ this -> resource ) ; if ( is_string ( $ source ) ) { fclose ( $ resource ) ; } }
Copies the contents of source.

If the source is seekable, copies content from beginning. Otherwise
copies data from current position.

@param string|resource|AbstractStream $source The source to copy

@throws \RuntimeException         If the internal resource is not
available
@throws \InvalidArgumentException

- If invalid source provided
- If source is not readable
Copies the contents of source .