Back to Stream class

Method open

public bool
open
(mixed $filename, mixed $mode = 'r', mixed $useIncludePath = false, mixed $context = null, mixed $usePrefix = false, mixed $relative = false, mixed $detectProcessingMode = false)
Generic File Operations
Parameters
  • string $filename Filename
  • string $mode Mode string to use
  • bool $useIncludePath Use the PHP include path
  • resource $context Context to use when opening
  • bool $usePrefix Use a prefix to open the file
  • bool $relative Filename is a relative path (if false, strips JPATH_ROOT to make it relative)
  • bool $detectProcessingMode Detect the processing method for the file and use the appropriate function to handle output automatically
Returns
  • bool
Since
  • 1.7.0
Class: Stream
Project: Joomla

Method open - Source code

/**
 * Generic File Operations
 *
 * Open a stream with some lazy loading smarts
 *
 * @param   string    $filename              Filename
 * @param   string    $mode                  Mode string to use
 * @param   boolean   $useIncludePath        Use the PHP include path
 * @param   resource  $context               Context to use when opening
 * @param   boolean   $usePrefix             Use a prefix to open the file
 * @param   boolean   $relative              Filename is a relative path (if false, strips JPATH_ROOT to make it relative)
 * @param   boolean   $detectProcessingMode  Detect the processing method for the file and use the appropriate function
 *                                           to handle output automatically
 *
 * @return  boolean
 *
 * @since   1.7.0
 */
public function open($filename, $mode = 'r', $useIncludePath = false, $context = null, $usePrefix = false, $relative = false, $detectProcessingMode = false)
{
    $filename = $this->_getFilename($filename, $mode, $usePrefix, $relative);
    if (!$filename) {
        $this->setError(Text::_('JLIB_FILESYSTEM_ERROR_STREAMS_FILENAME'));
        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 (FilesystemHelper::isJoomlaStream($url['scheme'])) {
            require_once __DIR__ . '/streams/' . $url['scheme'] . '.php';
        }
        // We have a scheme! force the method to be f
        $this->processingmethod = 'f';
    } elseif ($detectProcessingMode) {
        $ext = strtolower(File::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) {
        // Gzip doesn't support contexts or streams
        case 'gz':
            $this->fh = gzopen($filename, $mode, $useIncludePath);
            break;
        // Bzip2 is much like gzip except it doesn't use the include path
        case 'bz':
            $this->fh = bzopen($filename, $mode);
            break;
        // Fopen can handle streams
        case 'f':
        default:
            // One supplied at open; overrides everything
            if ($context) {
                $this->fh = fopen($filename, $mode, $useIncludePath, $context);
            } elseif ($this->context) {
                $this->fh = fopen($filename, $mode, $useIncludePath, $this->context);
            } else {
                $this->fh = fopen($filename, $mode, $useIncludePath);
            }
            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;
}