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

Breadcrumb

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

function Filesystem::dumpFile

Atomically dumps content into a file.

Parameters

string|resource $content The data to write into the file:

Throws

IOException if the file cannot be written to

File

vendor/symfony/filesystem/Filesystem.php, line 646

Class

Filesystem
Provides basic utility to manipulate the file system.

Namespace

Symfony\Component\Filesystem

Code

public function dumpFile(string $filename, $content) : void {
    if (\is_array($content)) {
        throw new \TypeError(\sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
    }
    $dir = \dirname($filename);
    if (is_link($filename) && ($linkTarget = $this->readlink($filename))) {
        $this->dumpFile(Path::makeAbsolute($linkTarget, $dir), $content);
        return;
    }
    if (!is_dir($dir)) {
        $this->mkdir($dir);
    }
    // Will create a temp file with 0600 access rights
    // when the filesystem supports chmod.
    $tmpFile = $this->tempnam($dir, basename($filename));
    try {
        if (false === self::box('file_put_contents', $tmpFile, $content)) {
            throw new IOException(\sprintf('Failed to write file "%s": ', $filename) . self::$lastError, 0, null, $filename);
        }
        self::box('chmod', $tmpFile, self::box('fileperms', $filename) ?: 0666 & ~umask());
        $this->rename($tmpFile, $filename, true);
    } finally {
        if (file_exists($tmpFile)) {
            if ('\\' === \DIRECTORY_SEPARATOR && !is_writable($tmpFile)) {
                self::box('chmod', $tmpFile, self::box('fileperms', $tmpFile) | 0200);
            }
            self::box('unlink', $tmpFile);
        }
    }
}
RSS feed
Powered by Drupal