Back to Stream class

Method close

public bool
close
()
Attempt to close a file handle
Returns
  • bool
Since
  • 1.7.0
Class: Stream
Project: Joomla

Method close - Source code

/**
 * Attempt to close a file handle
 *
 * Will return false if it failed and true on success
 * If the file is not open the system will return true, this function destroys the file handle as well
 *
 * @return  boolean
 *
 * @since   1.7.0
 */
public function close()
{
    if (!$this->fh) {
        $this->setError(Text::_('JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN'));
        return true;
    }
    $retval = false;
    // Capture PHP errors
    $php_errormsg = 'Error Unknown';
    $track_errors = ini_get('track_errors');
    ini_set('track_errors', true);
    switch ($this->processingmethod) {
        case 'gz':
            $res = gzclose($this->fh);
            break;
        case 'bz':
            $res = bzclose($this->fh);
            break;
        case 'f':
        default:
            $res = fclose($this->fh);
            break;
    }
    if (!$res) {
        $this->setError($php_errormsg);
    } else {
        // Reset this
        $this->fh = null;
        $retval = true;
    }
    // If we wrote, chmod the file after it's closed
    if ($this->openmode[0] == 'w') {
        $this->chmod();
    }
    // Restore error tracking to what it was before
    ini_set('track_errors', $track_errors);
    // Return the result
    return $retval;
}