You are here

public function FeedsFetcherResult::sanitizeFile in Feeds 8.2

Sanitize the file in place. Currently supported sanitizations:

  • Remove BOM header from UTF-8 files.

Parameters

string $filepath: The file path of the file to be sanitized.

Return value

The file path of the sanitized file.

2 calls to FeedsFetcherResult::sanitizeFile()
FeedsFetcherResult::getFilePath in lib/Drupal/feeds/FeedsFetcherResult.php
Get a path to a temporary file containing the resource provided by the fetcher.
FeedsFileFetcherResult::getFilePath in lib/Drupal/feeds/FeedsFileFetcherResult.php
Overrides parent::getFilePath().

File

lib/Drupal/feeds/FeedsFetcherResult.php, line 88

Class

FeedsFetcherResult
Base class for all fetcher results.

Namespace

Drupal\feeds

Code

public function sanitizeFile($filepath) {
  $handle = fopen($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)) {
    $contents = file_get_contents($filepath);
    $contents = substr($contents, 3);
    $status = file_put_contents($filepath, $contents);
    if ($status === FALSE) {
      throw new Exception(t('File @filepath is not writeable.', array(
        '@filepath' => $filepath,
      )));
    }
  }
  return $filepath;
}