Actions

API16

JStream/open

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

Open a stream with some lazy loading smarts

[Edit Descripton]

Description:JStream/open

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;
}

[Edit See Also] SeeAlso:JStream/open

Examples

<CodeExamplesForm />