You are here

public function WritableStreamBackupFile::openForWrite in Backup and Migrate 5.0.x

Open a file for reading or writing.

Parameters

bool $binary: Is the file binary.

Throws

\Exception

Overrides BackupFileWritableInterface::openForWrite

2 calls to WritableStreamBackupFile::openForWrite()
WritableStreamBackupFile::write in src/Core/File/WritableStreamBackupFile.php
Write a line to the file.
WritableStreamBackupFile::writeAll in src/Core/File/WritableStreamBackupFile.php
Open the file, writes the given contents and closes it.

File

src/Core/File/WritableStreamBackupFile.php, line 29

Class

WritableStreamBackupFile
A file object which represents an existing PHP stream with read/write.

Namespace

Drupal\backup_migrate\Core\File

Code

public function openForWrite($binary = FALSE) {
  if (!$this
    ->isOpen()) {
    $path = $this
      ->realpath();

    // Check if the file can be read/written.
    if (file_exists($path) && !is_writable($path) || !file_exists($path) && !is_writable(dirname($path))) {

      // @todo Throw better exception
      throw new BackupMigrateException('Cannot write to file: %path', [
        '%path' => $path,
      ]);
    }

    // Open the file.
    $mode = "w" . ($binary ? "b" : "");
    $this->handle = fopen($path, $mode);
    if (!$this->handle) {
      throw new BackupMigrateException('Cannot open file: %path', [
        '%path' => $path,
      ]);
    }
  }
}