Back to File class

Method write

public static bool
write
(mixed $file, mixed $buffer, mixed $useStreams = false)
Write 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
  • 1.7.0
Class: File
Project: Joomla

Method write - Source code

/**
 * Write 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   1.7.0
 */
public static function write($file, $buffer, $useStreams = false)
{
    @set_time_limit(ini_get('max_execution_time'));
    // If the destination directory doesn't exist we need to create it
    if (!file_exists(\dirname($file))) {
        if (Folder::create(\dirname($file)) == false) {
            return false;
        }
    }
    if ($useStreams) {
        $stream = Factory::getStream();
        // Beef up the chunk size to a meg
        $stream->set('chunksize', 1024 * 1024);
        if (!$stream->writeFile($file, $buffer)) {
            Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_WRITE_STREAMS', __METHOD__, $file, $stream->getError()), Log::WARNING, 'jerror');
            return false;
        }
        self::invalidateFileCache($file);
        return true;
    } else {
        $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->write($file, $buffer);
        } else {
            $file = Path::clean($file);
            $ret = \is_int(file_put_contents($file, $buffer));
        }
        self::invalidateFileCache($file);
        return $ret;
    }
}