How can I export all users in a CSV format? A site we have over 8000+ users with Views, Ctools and VBO installed. Is it possible to export all users data into 1 CSV file? We tired Views Data Export with no luck(memory limit), even we increase the memory to 128MB. Please help.
Stick the following in a file named expusers.module

&lt;?php
/**
 *  Implementation of hook_menu()
 */
function expusers_menu() {
  $items['expusers'] = array(
    'page callback' =&gt; '_expusers_select',
    'access arguments' =&gt; array('administer expusers'),
  );
  return $items;
}

/**
 * Callback
 */
function _expusers_select() {
  // 
  $csvfile_name = 'expusers.csv'; // the file to save to
  $users = array();
  $result = db_query('SELECT * FROM {users}');
  foreach ($result as $record) {
    unset($userarray);
    foreach ($record as $key =&gt; $value) {
      $userarray[] = (empty($value)) ? '' : '"' . $value . '"';
    }
    $user = implode(',',$userarray);
    $users[] = $user . PHP_EOL; // Remove this if file_save_data don't work.
    // Use PHP statement below to append to file here if file_save_data don't work.
    // file_put_contents(..path to file.., $user, FILE_APPEND); 
  }
  $file = file_save_data($users,'public://' .$csvfile_name); // Remove this if file_save_data don't work.
  return '&lt;p&gt;CSV dump of {users} table written to file "' . $file-&gt;uri . '".&lt;/p&gt;';
}


Stick the following in a file named expusers.info;

name = Export users
description = "Exports the {users} table to a CSV file in the public file area."
core = 7.x


Create a directory named expusers in your in your modules directory, and place both files in it.

Enable the custom module "Export users" on the site's module page.

Navigate to the path expusers (e.g. http://example.com/expusers).

The message:

CSV dump of {users} table written to file "public://expusers.csv".


Should appear.

Go to your public file area and pick up your file.

The above tiny custom module should be far less resource hungry than Views data export, so, hopefully, you will get your CSV without hitting a memory limit .

However, it first collects all the user data into an array in memory, before dumping into the file.

If you still hit a memory limit, you can't use Drupal's very convenient file_save_data to save to a file.  Instead, you must save the data one line at a time.  There is no built-in Drupal function to do this, but you should be able to use PHPs file_put_contents instead.

I've used comment to indicate how to change the code to replace Drupal's file_save_data  with PHP's file_put_contents.