JStream/open: Difference between revisions
From Joomla! Documentation
m clean up |
m preparing for archive only |
||
| Line 4: | Line 4: | ||
===Syntax=== | ===Syntax=== | ||
| Line 133: | Line 133: | ||
===Examples=== | ===Examples=== | ||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
category=open | category=open | ||
category=JStream | category=JStream | ||
namespace=CodeExample | |||
category=MethodExample | category=MethodExample | ||
include=* | include=* | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
Latest revision as of 02:07, 25 March 2017
Description
Open a stream with some lazy loading smarts
Syntax
open($filename, $mode='r', $use_include_path=false, $context=null, $use_prefix=true, $relative=false, $detectprocessingmode=false)
| Parameter Name | Default Value | Description |
|---|---|---|
| $filename | Filename | |
| $mode | 'r' | Mode string to use |
| $use_include_path | false | Use the PHP include path |
| $context | null | Context to use when opening |
| $use_prefix | true | Use a prefix to open the file |
| $relative | false | Filename is a relative path (if false, strips JPATH_ROOT to make it relative) |
| $detectprocessingmode | false | Detect the processing method for the file and use the appropriate function to handle output automatically |
Defined in
libraries/joomla/filesystem/stream.php
Importing
jimport( 'joomla.filesystem.stream' );
Source Body
function open($filename, $mode='r', $use_include_path=false, $context=null, $use_prefix=true, $relative=false, $detectprocessingmode=false)
{
$filename = $this->_getFilename($filename, $mode, $use_prefix, $relative);
if(!$filename)
{
$this->setError(JText::_('No filename set'));
return false;
}
$this->filename = $filename;
$this->_openmode = $mode;
$url = parse_url($filename);
$retval = false;
if(isset($url['scheme']))
{
// if we're dealing with a Joomla! stream, load it
if(JFilesystemHelper::isJoomlaStream($url['scheme'])) {
require_once dirname(__FILE__).DS.'streams'.DS.$url['scheme'].'.php';
}
// we have a scheme! force the method to be f
$this->processingmethod = 'f';
}
else if($detectprocessingmode)
{
$ext = strtolower(JFile::getExt($this->filename));
switch ($ext)
{
case 'tgz':
case 'gz':
case 'gzip':
$this->processingmethod = 'gz';
break;
case 'tbz2':
case 'bz2':
case 'bzip2':
$this->processingmethod = 'bz';
break;
default:
$this->processingmethod = 'f';
break;
}
}
// Capture PHP errors
$php_errormsg = 'Error Unknown whilst opening a file';
$track_errors = ini_get('track_errors');
ini_set('track_errors', true);
// Decide which context to use:
switch($this->processingmethod)
{
case 'gz': // gzip doesn't support contexts or streams
$this->_fh = gzopen($filename, $mode, $use_include_path);
break;
case 'bz': // bzip2 is much like gzip except it doesn't use the include path
$this->_fh = bzopen($filename, $mode);
break;
case 'f': // fopen can handle streams
default:
if($context) { // one supplied at open; overrides everything
$this->_fh = fopen($filename, $mode, $use_include_path, $context);
} else if ($this->_context) { // one provided at initialisation
$this->_fh = fopen($filename, $mode, $use_include_path, $this->_context);
} else { // no context; all defaults
$this->_fh = fopen($filename, $mode, $use_include_path);
}
break;
}
if(!$this->_fh) {
$this->setError($php_errormsg);
} else {
$retval = true;
}
// restore error tracking to what it was before
ini_set('track_errors',$track_errors);
// return the result
return $retval;
}
Examples
Code Examples