< API16:JStream

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.


File write Note: Whilst this function accepts a reference, the underlying fwrite will do a copy! This will roughly double the memory allocation for any write you do. Specifying chunked will get around this by only writing in specific chunk sizes. This defaults to 8192 which is a sane number to use most of the time (change the default with JStream::set('chunksize', newsize);) Note: This doesn't support gzip/bzip2 writing like reading does


write(&$string, $length=0, $chunk=0)
Parameter Name Default Value Description
&$string Reference to the string to write
$length 0 Length of the string to write
$chunk 0 Size of chunks to write in

Defined in



jimport( '' );

Source Body

function write(&$string, $length=0, $chunk=0)
                $this->setError(JText::_('File not open'));
                return false;
        // if the length isn't set, set it to the length of the string
        if(!$length) $length = strlen($string);
        // if the chunk isn't set, set it to the default
        if(!$chunk) $chunk = $this->chunksize;
        $retval = true;
        // Capture PHP errors
        $php_errormsg = '';
        $track_errors = ini_get('track_errors');
        ini_set('track_errors', true);
        $remaining = $length;
        do {
                // if the amount remaining is greater than the chunk size, then use the chunk
                $amount = ($remaining > $chunk) ? $chunk : $remaining;
                $res = fwrite($this->_fh, $string, $amount);
                // returns false on error or the number of bytes written
                if($res === false)
                { // returned error
                        $retval = false;
                        $remaining = 0;
                else if($res === 0)
                { // wrote nothing?
                        $remaining = 0;
                        $this->setError('Warning: No data written');
                } else
                { // wrote something
                        $remaining -= $res;
        } while($remaining);
        // restore error tracking to what it was before
        // return the result
        return $retval;


Code Examples