Horde_Mime_Part::_writeStream PHP Method

_writeStream() protected method

Write data to a stream.
protected _writeStream ( array $data, array $options = [] ) : resource
$data array The data to write. Either a stream resource or a string.
$options array Additional options: - error: (boolean) Catch errors when writing to the stream. Throw an ErrorException if an error is found. DEFAULT: false - filter: (array) Filter(s) to apply to the string. Keys are the filter names, values are filter params. - fp: (resource) Use this stream instead of creating a new one.
return resource The stream resource.
    protected function _writeStream($data, $options = array())
    {
        if (empty($options['fp'])) {
            $fp = fopen('php://temp/maxmemory:' . self::$memoryLimit, 'r+');
        } else {
            $fp = $options['fp'];
            fseek($fp, 0, SEEK_END);
        }
        if (!is_array($data)) {
            $data = array($data);
        }
        $append_filter = array();
        if (!empty($options['filter'])) {
            foreach ($options['filter'] as $key => $val) {
                $append_filter[] = stream_filter_append($fp, $key, STREAM_FILTER_WRITE, $val);
            }
        }
        if (!empty($options['error'])) {
            set_error_handler(function ($errno, $errstr) {
                throw new ErrorException($errstr, $errno);
            });
            $error = null;
        }
        try {
            reset($data);
            while (list(, $d) = each($data)) {
                if (is_resource($d)) {
                    rewind($d);
                    while (!feof($d)) {
                        fwrite($fp, fread($d, 8192));
                    }
                } elseif (is_string($d)) {
                    $len = strlen($d);
                    $i = 0;
                    while ($i < $len) {
                        fwrite($fp, substr($d, $i, 8192));
                        $i += 8192;
                    }
                }
            }
        } catch (ErrorException $e) {
            $error = $e;
        }
        foreach ($append_filter as $val) {
            stream_filter_remove($val);
        }
        if (!empty($options['error'])) {
            restore_error_handler();
            if ($error) {
                throw $error;
            }
        }
        return $fp;
    }