You are here

public function TaskManager::toCsv in General Data Protection Regulation 8

Same name and namespace in other branches
  1. 8.2 modules/gdpr_tasks/src/TaskManager.php \Drupal\gdpr_tasks\TaskManager::toCsv()
  2. 3.0.x modules/gdpr_tasks/src/TaskManager.php \Drupal\gdpr_tasks\TaskManager::toCsv()

Writes array data to a csv file.

Parameters

array $data: The data to be stored in csv.

string $dirname: The local path or stream wrapper for destination directory.

Return value

string The uri path of the created file.

File

modules/gdpr_tasks/src/TaskManager.php, line 94

Class

TaskManager
Defines a helper class for stuff related to views data.

Namespace

Drupal\gdpr_tasks

Code

public function toCsv(array $data, $dirname = 'private://') {

  // Prepare destination.
  file_prepare_directory($dirname, FILE_CREATE_DIRECTORY);

  // Generate a file entity.
  $random = new Random();
  $destination = $dirname . '/' . $random
    ->name(10, TRUE) . '.csv';

  // Update csv with actual data.
  $fp = fopen($destination, 'w');
  foreach ($data as $line) {
    fputcsv($fp, $line);
  }
  fclose($fp);
  return $destination;
}