You are here

public static function FeedImportFilter::saveFile in Feed Import 8

Same name in this branch
  1. 8 feed_import_base/filters/feed_import_default_filters.php \FeedImportFilter::saveFile()
  2. 8 feed_import_base/src/Filter/FeedImportFilter.php \Drupal\feed_import_base\FeedImportFilter::saveFile()

Downloads and saves a file in a field

Parameters

mixed $field: A string or an array of strings

string $path: Where to save file. Default is public://

int $options: Use 0 to rename existing file or 1 to replace it.

Return value

mixed An object or an array of objects containing file info

1 call to FeedImportFilter::saveFile()
FeedImportFilter::saveImage in feed_import_base/src/Filter/FeedImportFilter.php
This is an alis for saveFile() function.

File

feed_import_base/src/Filter/FeedImportFilter.php, line 647

Class

FeedImportFilter
This class contains default filters for feed import.

Namespace

Drupal\feed_import_base

Code

public static function saveFile($field, $path = 'public://', $options = FILE_EXISTS_RENAME) {
  if (is_array($field)) {
    foreach ($field as &$f) {
      $f = self::saveFile($f, $path, $options);
    }
    return $field;
  }

  // Get file data.
  try {
    $image = file_get_contents($field);
  } catch (Exception $e) {
    return NULL;
  }
  $field = trim($field, '/');
  $field = \Drupal\Component\Utility\Unicode::substr($field, strrpos($field, '/') + 1);
  if (file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    return file_save_data($image, $path . $field, (int) $options);
  }
  return NULL;
}