API16:JStream/read
From Joomla! Documentation
Description
Read a file Handles user space streams appropriately otherwise any read will return 8192
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;
}
Examples
Code Examples