Actions

API16

JStream/read

From Joomla! Documentation

< API16:JStream
Revision as of 17:47, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Read a file Handles user space streams appropriately otherwise any read will return 8192

[Edit Descripton]

Description:JStream/read

Syntax

read($length=0)
Parameter Name Default Value Description
$length 0 length of data to read

Defined in

libraries/joomla/filesystem/stream.php

Importing

jimport( 'joomla.filesystem.stream' );

Source Body

function read($length=0)
{
        if(!$this->_filesize && !$length)
        {
                $this->filesize(); // get the filesize
                if(!$this->_filesize) {
                        $length = -1; // set it to the biggest and then wait until eof
                } else {
                        $length = $this->_filesize;
                }
        }
        if(!$this->_fh)
        {
                $this->setError(JText::_('File not open'));
                return false;
        }
        $retval = false;
        // Capture PHP errors
        $php_errormsg = 'Error Unknown';
        $track_errors = ini_get('track_errors');
        ini_set('track_errors', true);
        $remaining = $length;
        do {
                // do chunked reads where relevant
                switch($this->processingmethod)
                {
                        case 'bz':
                                $res = ($remaining > 0) ? bzread($this->_fh, $remaining) : bzread($this->_fh, $this->chunksize);
                                break;
                        case 'gz':
                                $res = ($remaining > 0) ? gzread($this->_fh, $remaining) : gzread($this->_fh, $this->chunksize);
                                break;
                        case 'f':
                        default:
                                $res = ($remaining > 0) ? fread($this->_fh, $remaining) : fread($this->_fh, $this->chunksize);
                                break;
                }
 
                if(!$res)
                {
                        $this->setError($php_errormsg);
                        $remaining = 0; // jump from the loop
                } else
                {
                        if(!$retval) $retval = '';
                        $retval .= $res;
                        if(!$this->eof())
                        {
                                $len = strlen($res);
                                $remaining -= $len;
                        }
                        else
                        {
                                // if its the end of the file then we've nothing left to read; reset remaining and len
                                $remaining = 0;
                                $length = strlen($retval);
                        }
                }
        } while($remaining || !$length);
        // restore error tracking to what it was before
        ini_set('track_errors',$track_errors);
        // return the result
        return $retval;
}

[Edit See Also] SeeAlso:JStream/read

Examples

<CodeExamplesForm />