You are here

function WritableStreamBackupFile::openForWrite in Backup and Migrate 8.4

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 lib/backup_migrate_core/src/File/WritableStreamBackupFile.php
Write a line to the file.
WritableStreamBackupFile::writeAll in lib/backup_migrate_core/src/File/WritableStreamBackupFile.php
A shorthand function to open the file, write the given contents and close the file. Used for small amounts of data that can fit in memory.

File

lib/backup_migrate_core/src/File/WritableStreamBackupFile.php, line 37

Class

WritableStreamBackupFile
Class TempFile.

Namespace

BackupMigrate\Core\File

Code

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,
      ]);
    }
  }
}