You are here

protected function FetcherResult::sanitizeFile in Feeds 8.3

Sanitizes the file in place.

Currently supported sanitizations:

  • Remove BOM header from UTF-8 files.

Return value

string The file path of the sanitized file.

Throws

\RuntimeException Thrown if the file is not writable.

1 call to FetcherResult::sanitizeFile()
FetcherResult::getFilePath in src/Result/FetcherResult.php
Returns the path to the file containing the file provided by the fetcher.

File

src/Result/FetcherResult.php, line 93

Class

FetcherResult
The default fetcher result object.

Namespace

Drupal\feeds\Result

Code

protected function sanitizeFile() {
  $handle = fopen($this->filePath, 'r');
  $line = fgets($handle);
  fclose($handle);

  // If BOM header is present, read entire contents of file and overwrite the
  // file with corrected contents.
  if (substr($line, 0, 3) !== pack('CCC', 0xef, 0xbb, 0xbf)) {
    return $this->filePath;
  }
  if (!is_writable($this->filePath)) {
    throw new \RuntimeException(new FormattableMarkup('File %filepath is not writable.', [
      '%filepath' => $this->filePath,
    ]));
  }
  $contents = file_get_contents($this->filePath);
  $contents = substr($contents, 3);
  $status = file_put_contents($this->filePath, $contents);
  return $this->filePath;
}