class InputStreamFileWriter

Same name and namespace in other branches
  1. 11.x core/modules/file/src/Upload/InputStreamFileWriter.php \Drupal\file\Upload\InputStreamFileWriter

Writes files from a input stream to a temporary file.

Hierarchy

Expanded class hierarchy of InputStreamFileWriter

1 string reference to 'InputStreamFileWriter'
file.services.yml in core/modules/file/file.services.yml
core/modules/file/file.services.yml
1 service uses InputStreamFileWriter
file.input_stream_file_writer in core/modules/file/file.services.yml
Drupal\file\Upload\InputStreamFileWriter

File

core/modules/file/src/Upload/InputStreamFileWriter.php, line 13

Namespace

Drupal\file\Upload
View source
class InputStreamFileWriter implements InputStreamFileWriterInterface {
  
  /**
   * Creates a new InputStreamFileUploader.
   */
  public function __construct(protected FileSystemInterface $fileSystem) {
  }
  
  /**
   * {@inheritdoc}
   */
  public function writeStreamToFile(string $stream = self::DEFAULT_STREAM, int $bytesToRead = self::DEFAULT_BYTES_TO_READ) : string {
    // 'rb' is needed so reading works correctly on Windows environments too.
    $fileData = fopen($stream, 'rb');
    $tempFilePath = $this->fileSystem
      ->tempnam('temporary://', 'file');
    $tempFile = fopen($tempFilePath, 'wb');
    if ($tempFile) {
      while (!feof($fileData)) {
        $read = fread($fileData, $bytesToRead);
        if ($read === FALSE) {
          // Close the file streams.
          fclose($tempFile);
          fclose($fileData);
          throw new UploadException('Input file data could not be read');
        }
        if (fwrite($tempFile, $read) === FALSE) {
          // Close the file streams.
          fclose($tempFile);
          fclose($fileData);
          throw new CannotWriteFileException(sprintf('Temporary file data for "%s" could not be written', $tempFilePath));
        }
      }
      // Close the temp file stream.
      fclose($tempFile);
    }
    else {
      // Close the input file stream since we can't proceed with the upload.
      // Don't try to close $tempFile since it's FALSE at this point.
      fclose($fileData);
      throw new NoFileException(sprintf('Temporary file "%s" could not be opened for file upload', $tempFilePath));
    }
    // Close the input stream.
    fclose($fileData);
    return $tempFilePath;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
InputStreamFileWriter::writeStreamToFile public function Write the input stream to a temporary file. Overrides InputStreamFileWriterInterface::writeStreamToFile
InputStreamFileWriter::__construct public function Creates a new InputStreamFileUploader.
InputStreamFileWriterInterface::DEFAULT_BYTES_TO_READ constant The length of bytes to read in each iteration when streaming file data.
InputStreamFileWriterInterface::DEFAULT_STREAM constant The default stream.

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