Skip to main content
1 of 4

When you are importing a large number of contacts, you could face timeout issues when using the user interface. Importing through CLI is an alternative.

There is a file called import.php in /civicrm/civicrm/bin/csv/import.php. We will call this file from the command line.

After you have exported your CSV file, here are the steps for the import:

  1. Put the CSV file in your site's root directory (or other convenient place).
  2. In the command prompt (shell), type php path/to/import.php -e Contact --file path/to/file.csv (check this forum post for some more detail). Provide an absolute path to the CSV file.
  3. At this point, depending on the contents of your CSV file, you will likely get an error such as ERROR line 2: Mandatory key(s) missing from params array: contact_type. It expects you to pass a contact_type parameter inside the CSV file. If you don't have one in your exported CSV file, you can very easily add a contact_type column in it using a Notepad++ macro (note: turn off autocompletion for words and quotes in Notepadd++ as it may screw up the macro). Just add the new column at the end. Here is an example of a CSV file:

"email","contact_type"
"[email protected]","Individual"
"[email protected]","Individual"

  1. Now run the command again and you should see a success message for each imported contact. The message looks like this: line 2: created Contact id: 114

The down side is that you cannot (yet) assign a group or a tag to the imported contacts. I looked into the API but could not see an ability to add contacts to groups or tags.


Troubleshooting:

If you get an error such as "cli.php can only be run from command line.", and you wonder about it because you ARE running from the command line, then likely you are using php as cgi/fcgi. To overcome this there is a little hack (which I am not too happy with, but we actually are using the code as intended here). In /bin/cli.class.php, on line 85 the function _accessing_from_cli() checks whether the program is run from CLI. It does this by checking the contents of PHP_SAPI. But according to the first comment here, that variable is not reliable in some cases. So we can modify the function like this:

public function _accessing_from_cli() { if (PHP_SAPI === 'cli') { return TRUE; } else { /* Temporary hack since the PHP_SAPI check does not work correctly for cgi/fcgi. See first comment here: http://php.net/manual/en/function.php-sapi-name.php */ return TRUE; //trigger_error("cli.php can only be run from command line.", E_USER_ERROR); } }

After you import your contacts, do not forget to return the file back to its original version.