Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. FileSecurity.php

function FileSecurity::writeFile

Same name in this branch
  1. 11.1.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.

1 call to FileSecurity::writeFile()
FileSecurity::writeHtaccess in composer/Plugin/VendorHardening/FileSecurity.php
Writes an .htaccess file in the given directory, if it doesn't exist.

File

composer/Plugin/VendorHardening/FileSecurity.php, line 118

Class

FileSecurity
Provides file security functions.

Namespace

Drupal\Composer\Plugin\VendorHardening

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;
    }
    // Try to write the file. This can fail if concurrent requests are both
    // trying to write a the same time.
    if (@file_put_contents($file_path, $contents)) {
        return @chmod($file_path, 0444);
    }
    return FALSE;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal