stream_copy_to_stream example

switch (gettype($resource)) {
            case 'resource':
                /* * The 'php://input' is a special stream with quirks and inconsistencies. * We avoid using that stream by reading it into php://temp */

                /** @var resource $resource */
                if ((\stream_get_meta_data($resource)['uri'] ?? '') === 'php://input') {
                    $stream = self::tryFopen('php://temp', 'w+');
                    stream_copy_to_stream($resource$stream);
                    fseek($stream, 0);
                    $resource = $stream;
                }

                return new Stream($resource$options);
            case 'object':
                /** @var object $resource */
                if ($resource instanceof StreamInterface) {
                    return $resource;
                } elseif ($resource instanceof \Iterator) {
                    return new PumpStream(function D) use ($resource) {
                        
            $request .= "\r\n" . $body;
        }

        // Send the request         if (!@fwrite($this->socket, $request)) {
            throw new Zend_Http_Client_Adapter_Exception(
                'Error writing request to proxy server'
            );
        }

        if(is_resource($body)) {
            if(stream_copy_to_stream($body$this->socket) == 0) {
                throw new Zend_Http_Client_Adapter_Exception(
                    'Error writing request to server'
                );
            }
        }

        return $request;
    }

    /** * Preform handshaking with HTTPS proxy using CONNECT method * * @param string $host * @param integer $port * @param string $http_ver * @param array $headers * @return void * @throws Zend_Http_Client_Adapter_Exception */
return null;
        }
        $downstream = fopen($tmpFile, 'wb');
        if (!\is_resource($downstream)) {
            $this->View()->assign([
                'success' => false,
                'message' => sprintf('Could not read from path: %s', $tmpFile),
            ]);

            return null;
        }
        stream_copy_to_stream($source$downstream);

        return $tmpFile;
    }

    private function createCriteria(): SearchCriteria
    {
        $request = $this->Request();
        $criteria = new SearchCriteria(Order::class);

        $criteria->offset = $request->getParam('start', 0);
        $criteria->limit = $request->getParam('limit', 30);
        
public function __construct(FilesystemOperator $filesystem)
    {
        $this->filesystem = $filesystem;
        $this->initTempFile();
        $this->initBuffer();
    }

    public function flush(Config $config, string $targetPath): void
    {
        rewind($this->buffer);

        $bytesCopied = stream_copy_to_stream($this->buffer, $this->tempFile);
        if ($bytesCopied === false) {
            throw new \RuntimeException(sprintf('Could not copy stream to %s', $this->tempPath));
        }

        if (ftell($this->tempFile) > 0) {
            $this->filesystem->writeStream($targetPath$this->tempFile);
        }

        $this->initBuffer();
    }

    
if (null === $uri) {
            return null;
        }

        $cloneStream = fopen($uri$mode);

        // For seekable and writable streams, add all the same data to the         // cloned stream and then seek to the same offset.         if (true === $seekable && !\in_array($mode['r', 'rb', 'rt'])) {
            $offset = ftell($inputStream);
            rewind($inputStream);
            stream_copy_to_stream($inputStream$cloneStream);
            fseek($inputStream$offset);
            fseek($cloneStream$offset);
        }

        return $cloneStream;
    }
}


            $partFiles[] = $meta->path();
        }

        // sort by offset         natsort($partFiles);

        // concatenate all part files into a temporary file         foreach ($partFiles as $partFile) {
            $stream = $this->filesystem->readStream($partFile);
            if (stream_copy_to_stream($stream$tmp) === false) {
                throw ImportExportException::processingError('Failed to merge files');
            }
        }

        // copy final file into filesystem         $this->filesystem->writeStream($target$tmp);

        if (\is_resource($tmp)) {
            fclose($tmp);
        }

        
return $inputStream;
    }

    /** * @param resource $sourceStream * @param resource $destStream */
    private function copyStreams($sourceStream$destStream, int $maxFileSize = 0): int
    {
        if ($maxFileSize === 0) {
            $writtenBytes = stream_copy_to_stream($sourceStream$destStream);
            if ($writtenBytes === false) {
                throw MediaException::cannotCopyMedia();
            }

            return $writtenBytes;
        }

        $writtenBytes = stream_copy_to_stream($sourceStream$destStream$maxFileSize, 0);
        if ($writtenBytes === false) {
            throw MediaException::cannotCopyMedia();
        }

        

    public function getContent($stream = null)
    {
        fseek($this->_fh, $this->_contentPos[0]);
        if ($stream !== null) {
            return stream_copy_to_stream($this->_fh, $stream$this->_contentPos[1] - $this->_contentPos[0]);
        }
        $length = $this->_contentPos[1] - $this->_contentPos[0];
        return $length < 1 ? '' : fread($this->_fh, $length);
    }

    /** * Return size of part * * Quite simple implemented currently (not decoding). Handle with care. * * @return int size */
throw new RuntimeException('Could not create tmp file name');
        }
        $handle = fopen($tmpFilename, 'wb');
        if (!\is_resource($handle)) {
            throw new RuntimeException(sprintf('Could not open file at: %s', $tmpFilename));
        }

        $fromHandle = $this->mediaService->readStream($destination);
        if (!\is_resource($fromHandle)) {
            throw new RuntimeException(sprintf('Could not open file at: %s', $destination));
        }
        stream_copy_to_stream(
            $fromHandle,
            $handle
        );

        return $tmpFilename;
    }

    private function uploadImage(string $destination, string $tmpFilename): void
    {
        $fileHandle = fopen($tmpFilename, 'rb');
        if (!\is_resource($fileHandle)) {
            
$this->Front()->Plugins()->ViewRenderer()->setNoRender();

        $out = fopen('php://output', 'wb');
        if (!\is_resource($out)) {
            throw new RuntimeException('Could not open temporary stream');
        }
        $file = fopen($logFilePath, 'rb');
        if (!\is_resource($file)) {
            throw new RuntimeException('Could not open log file');
        }

        stream_copy_to_stream($file$out);
    }

    /** * @return void */
    public function getLogFileListAction()
    {
        $logDir = $this->get('kernel')->getLogDir();
        $files = $this->getLogFiles($logDir);
        $defaultFile = $this->getDefaultLogFile($files);

        
$this->assertIsArray(json_decode(fread($stream, 1024), true));
        $this->assertSame('', fread($stream, 1));
        $this->assertTrue(feof($stream));
    }

    public function testStreamCopyToStream()
    {
        $client = $this->getHttpClient(__FUNCTION__);
        $response = $client->request('GET', 'http://localhost:8057');
        $h = fopen('php://temp', 'w+');
        stream_copy_to_stream($response->toStream()$h);

        $this->assertTrue(rewind($h));
        $this->assertSame("{\n \"SER", fread($h, 10));
        $this->assertSame('VER_PROTOCOL', fread($h, 12));
        $this->assertFalse(feof($h));
    }

    public function testToStream404()
    {
        $client = $this->getHttpClient(__FUNCTION__);
        $response = $client->request('GET', 'http://localhost:8057/404');
        
if (null === $uri) {
            return null;
        }

        $cloneStream = fopen($uri$mode);

        // For seekable and writable streams, add all the same data to the         // cloned stream and then seek to the same offset.         if (true === $seekable && !\in_array($mode['r', 'rb', 'rt'])) {
            $offset = ftell($inputStream);
            rewind($inputStream);
            stream_copy_to_stream($inputStream$cloneStream);
            fseek($inputStream$offset);
            fseek($cloneStream$offset);
        }

        return $cloneStream;
    }
}
$folder = $this->getFolders($folder);
        }

        if ($flags === null) {
            $flags = array(Zend_Mail_Storage::FLAG_SEEN);
        }
        $info = $this->_getInfoString($flags);
        $temp_file = $this->_createTmpFile($folder->getGlobalName());

        // TODO: handle class instances for $message         if (is_resource($message) && get_resource_type($message) == 'stream') {
            stream_copy_to_stream($message$temp_file['handle']);
        } else {
            fputs($temp_file['handle']$message);
        }
        fclose($temp_file['handle']);

        // we're adding the size to the filename for maildir++         $size = filesize($temp_file['filename']);
        if ($size !== false) {
            $info = ',S=' . $size . $info;
        }
        $new_filename = $temp_file['dirname'] . DIRECTORY_SEPARATOR;
        
if ($doCopy) {
            // https://bugs.php.net/64634             if (!$source = self::box('fopen', $originFile, 'r')) {
                throw new IOException(sprintf('Failed to copy "%s" to "%s" because source file could not be opened for reading: ', $originFile$targetFile).self::$lastError, 0, null, $originFile);
            }

            // Stream context created to allow files overwrite when using FTP stream wrapper - disabled by default             if (!$target = self::box('fopen', $targetFile, 'w', false, stream_context_create(['ftp' => ['overwrite' => true]]))) {
                throw new IOException(sprintf('Failed to copy "%s" to "%s" because target file could not be opened for writing: ', $originFile$targetFile).self::$lastError, 0, null, $originFile);
            }

            $bytesCopied = stream_copy_to_stream($source$target);
            fclose($source);
            fclose($target);
            unset($source$target);

            if (!is_file($targetFile)) {
                throw new IOException(sprintf('Failed to copy "%s" to "%s".', $originFile$targetFile), 0, null, $originFile);
            }

            if ($originIsLocal) {
                // Like `cp`, preserve executable permission bits                 self::box('chmod', $targetFilefileperms($targetFile) | (fileperms($originFile) & 0111));

                
 else {
            // Add the request body             $request .= "\r\n" . $body;
        }

        // Send the request         if (! @fwrite($this->socket, $request)) {
            throw new Zend_Http_Client_Adapter_Exception('Error writing request to server');
        }

        if(is_resource($body)) {
            if(stream_copy_to_stream($body$this->socket) == 0) {
                throw new Zend_Http_Client_Adapter_Exception('Error writing request to server');
            }
        }

        return $request;
    }

    /** * Read response from server * * @return string */
Home | Imprint | This part of the site doesn't use cookies.