function FileSecurity::writeFile

Same name in this branch
  1. 8.9.x composer/Plugin/VendorHardening/FileSecurity.php \Drupal\Composer\Plugin\VendorHardening\FileSecurity::writeFile()
Same name in other branches
  1. 9 composer/Plugin/VendorHardening/FileSecurity.php \Drupal\Composer\Plugin\VendorHardening\FileSecurity::writeFile()
  2. 9 core/lib/Drupal/Component/FileSecurity/FileSecurity.php \Drupal\Component\FileSecurity\FileSecurity::writeFile()
  3. 10 composer/Plugin/VendorHardening/FileSecurity.php \Drupal\Composer\Plugin\VendorHardening\FileSecurity::writeFile()
  4. 10 core/lib/Drupal/Component/FileSecurity/FileSecurity.php \Drupal\Component\FileSecurity\FileSecurity::writeFile()
  5. 11.x composer/Plugin/VendorHardening/FileSecurity.php \Drupal\Composer\Plugin\VendorHardening\FileSecurity::writeFile()
  6. 11.x core/lib/Drupal/Component/FileSecurity/FileSecurity.php \Drupal\Component\FileSecurity\FileSecurity::writeFile()

Writes the contents to the file in the given directory.

Parameters

string $directory: The directory to write to.

string $filename: The file name.

string $contents: The file contents.

bool $force: TRUE if we should force the write over an existing file.

Return value

bool TRUE if writing the file was successful.

2 calls to FileSecurity::writeFile()
FileSecurity::writeHtaccess in core/lib/Drupal/Component/FileSecurity/FileSecurity.php
Writes an .htaccess file in the given directory, if it doesn't exist.
FileSecurity::writeWebConfig in core/lib/Drupal/Component/FileSecurity/FileSecurity.php
Writes a web.config file in the given directory, if it doesn't exist.

File

core/lib/Drupal/Component/FileSecurity/FileSecurity.php, line 149

Class

FileSecurity
Provides file security functions.

Namespace

Drupal\Component\FileSecurity

Code

protected static function writeFile($directory, $filename, $contents, $force) {
    $file_path = $directory . DIRECTORY_SEPARATOR . $filename;
    // Don't overwrite if the file exists unless forced.
    if (file_exists($file_path) && !$force) {
        return TRUE;
    }
    // Writing the file can fail if:
    // - concurrent requests are both trying to write at the same time.
    // - $directory does not exist or is not writable.
    // Testing for these conditions introduces windows for concurrency issues to
    // occur.
    if (@file_put_contents($file_path, $contents)) {
        return @chmod($file_path, 0444);
    }
    return FALSE;
}

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