function file_save_htaccess in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/includes/file.inc \file_save_htaccess()
Creates a .htaccess file in the given directory.
Parameters
string $directory: The directory.
bool $private: (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.
Related topics
6 calls to file_save_htaccess()
- FileSaveHtaccessLoggingTest::testHtaccessSave in core/
modules/ system/ src/ Tests/ File/ FileSaveHtaccessLoggingTest.php - Tests file_save_htaccess().
- FileStorage::ensureStorage in core/
lib/ Drupal/ Core/ Config/ FileStorage.php - Check if the directory exists and create it if not.
- file_ensure_htaccess in core/
includes/ file.inc - Creates a .htaccess file in each Drupal files directory if it is missing.
- HtaccessUnitTest::testHtaccessSave in core/
modules/ system/ src/ Tests/ File/ HtaccessUnitTest.php - Tests file_save_htaccess().
- simpletest_requirements in core/
modules/ simpletest/ simpletest.install - Implements hook_requirements().
File
- core/
includes/ file.inc, line 350 - API for handling file uploads and server file management.
Code
function file_save_htaccess($directory, $private = TRUE, $force_overwrite = FALSE) {
if (\Drupal::service('file_system')
->uriScheme($directory)) {
$htaccess_path = file_stream_wrapper_uri_normalize($directory . '/.htaccess');
}
else {
$directory = rtrim($directory, '/\\');
$htaccess_path = $directory . '/.htaccess';
}
if (file_exists($htaccess_path) && !$force_overwrite) {
// Short circuit if the .htaccess file already exists.
return TRUE;
}
$htaccess_lines = FileStorage::htaccessLines($private);
// Write the .htaccess file.
if (file_exists($directory) && is_writable($directory) && file_put_contents($htaccess_path, $htaccess_lines)) {
return drupal_chmod($htaccess_path, 0444);
}
else {
$variables = array(
'%directory' => $directory,
'@htaccess' => $htaccess_lines,
);
\Drupal::logger('security')
->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>", $variables);
return FALSE;
}
}