Back to File class

Method append

public static bool
append
(mixed $file, mixed $buffer, mixed $useStreams = false)
Append contents to a file
Parameters
  • string $file The full file path
  • string $buffer The buffer to write
  • bool $useStreams Use streams
Returns
  • bool True on success
Since
  • 3.6.0
Class: File
Project: Joomla

Method append - Source code

/**
 * Append contents to a file
 *
 * @param   string   $file        The full file path
 * @param   string   $buffer      The buffer to write
 * @param   boolean  $useStreams  Use streams
 *
 * @return  boolean  True on success
 *
 * @since   3.6.0
 */
public static function append($file, $buffer, $useStreams = false)
{
    @set_time_limit(ini_get('max_execution_time'));
    // If the file doesn't exist, just write instead of append
    if (!file_exists($file)) {
        return self::write($file, $buffer, $useStreams);
    }
    if ($useStreams) {
        $stream = Factory::getStream();
        // Beef up the chunk size to a meg
        $stream->set('chunksize', 1024 * 1024);
        if ($stream->open($file, 'ab') && $stream->write($buffer) && $stream->close()) {
            self::invalidateFileCache($file);
            return true;
        }
        Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WRITE_STREAMS', __METHOD__, $file, $stream->getError()), Log::WARNING, 'jerror');
        return false;
    } else {
        // Initialise variables.
        $FTPOptions = ClientHelper::getCredentials('ftp');
        if ($FTPOptions['enabled'] == 1) {
            // Connect the FTP client
            $ftp = FtpClient::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
            // Translate path for the FTP account and use FTP write buffer to file
            $file = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
            $ret = $ftp->append($file, $buffer);
        } else {
            $file = Path::clean($file);
            $ret = \is_int(file_put_contents($file, $buffer, FILE_APPEND));
        }
        self::invalidateFileCache($file);
        return $ret;
    }
}