function HtaccessWriter::write

Same name in other branches
  1. 9 core/lib/Drupal/Core/File/HtaccessWriter.php \Drupal\Core\File\HtaccessWriter::write()
  2. 10 core/lib/Drupal/Core/File/HtaccessWriter.php \Drupal\Core\File\HtaccessWriter::write()
  3. 11.x core/lib/Drupal/Core/File/HtaccessWriter.php \Drupal\Core\File\HtaccessWriter::write()

Creates a .htaccess file in the given directory.

@internal

Parameters

string $directory: The directory.

bool $deny_public_access: (Optional) FALSE indicates that $directory should be a web-accessible directory. Defaults to TRUE which indicates a private directory.

bool $force_overwrite: (Optional) Set to TRUE to attempt to overwrite the existing .htaccess file if one is already present. Defaults to FALSE.

Return value

bool TRUE if the .htaccess file was saved or already exists, FALSE otherwise.

See also

\Drupal\Component\FileSecurity\FileSecurity::writeHtaccess()

1 call to HtaccessWriter::write()
HtaccessWriter::ensure in core/lib/Drupal/Core/File/HtaccessWriter.php
Creates a .htaccess file in each Drupal files directory if it is missing.

File

core/lib/Drupal/Core/File/HtaccessWriter.php, line 85

Class

HtaccessWriter
Provides functions to manage Apache .htaccess files.

Namespace

Drupal\Core\File

Code

public function write($directory, $deny_public_access = TRUE, $force_overwrite = FALSE) {
    if (StreamWrapperManager::getScheme($directory)) {
        $directory = $this->streamWrapperManager
            ->normalizeUri($directory);
    }
    else {
        $directory = rtrim($directory, '/\\');
    }
    if (FileSecurity::writeHtaccess($directory, $deny_public_access, $force_overwrite)) {
        return TRUE;
    }
    $this->logger
        ->error("Security warning: Couldn't write .htaccess file. Please create a .htaccess file in your %directory directory which contains the following lines: <pre><code>@htaccess</code></pre>", [
        '%directory' => $directory,
        '@htaccess' => FileSecurity::htaccessLines($deny_public_access),
    ]);
    return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.