I have been trying to import some users with custom attributes (eg firstname, last name, company, title) from some existing excel file, and I bumped into some problems. Browsing this forum, I could get going by turning my csv files from excel into text files, TAB-separated, but even then the import would fail as some of the imported data contained : or , or other special characters. I had to remove them to be able to import, which was not acceptable in some cases. So I looked into it, and I gathered some pieces of codes from the internet that did the trick :
1 - Prepare data :
- The goal is to import an excel data file. As an example, let's say the fields are : email,firstname, lastname, company, title. They need not be in that order, even though for all trials I did they were in this order.
- The fields name must be on the first line. Case does not matter, even though again I have not checked.
- Save the file as a csv file. Because of Excel using various locale separator, and also not enclosing fields with , or ; in double quotes, I suggest you use the following excel macro to create the properly formatted csv instead of the Save as... function from excel :
- Code: Select all
Sub SaveAsCSV()
Dim Range As Object, Line As Object, Cell As Object
Dim StrTemp As String
Dim Separateur As String
Separateur = ","
Set Range = ActiveSheet.UsedRange
Open "Export.csv" For Output As #1
For Each Line In Range.Rows
StrTemp = ""
For Each Cell In Line.Cells
StrTemp = StrTemp & Chr(34) & Trim(CStr _
(Cell.Text)) & Chr(34) & Separateur
Next
Print #1, Mid(StrTemp, 1, Len(StrTemp) - 1) '= " "
Next
Close
End Sub
This macro will create a csv file named export.csv (in the current directory) based on the current spreadsheet data. The content of each cell will be enclosed between double-quotes, so as to preserve special characters. The file will look like :
"email","firstname", "lastname", "company", "title"
"theboss@company.com","Johnny left","is Right","Our company","Mng:of:Business"
"thehacker@company.com","buddy","Goodman","Our company","Mkr:of:Business"
.....
.....
2 - Hack phplist
This is only for last version as of today, 2.10.2. I do not know about other version.
You should edit the file /lists/admin/import1.php
- Around line 90, you will find the following code. Comment it out by adding a double forward slash (//) at the beginning of each line, as shown below
- Code: Select all
// Check file for illegal characters
// $illegal_cha = array(",", ";", ":", "#","\t");
//for($i=0; $i<count($illegal_cha); $i++) {
//if( ($illegal_cha[$i] != $import_field_delimiter) && ($illegal_cha[$i] != $import_record_delimiter) && (strpos($email_list, $illegal_cha[$i]) != false) ) {
//Fatal_Error($GLOBALS['I18N']->get('invalid_delimiter')." $import_field_delimiter, $import_record_delimiter");return;
//}
//};
Please note code shown here is after adding the //. The original code is without these.
- On line 14, just after the line ob_end_flush(), insert the following code :
- Code: Select all
// shumisha 2006-04-06
function csv_string_to_array($str){
$expr="/,(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
$results=preg_split($expr,trim($str));
return preg_replace("/^\"(.*)\"$/","$1",$results);}
Please note that this code is not from me,it is a piece of code displayed on this page : http://fr.php.net/fgetcsv by an anonymous poster.
- approx. on line 110, replace the line
- Code: Select all
$uservalues = explode($import_field_delimiter,$line);
by the line
- Code: Select all
$uservalues = csv_string_to_array($line);
Save the file back and you should be able to import now.
3 - Import your csv file
- in phplist, got to Manageusers, then select import users.
- select import emails with different values for attributes. The hack should also work with the second option (import emails with the same values for attributes), but there is another bug there, and attributes seems not to be correctly populated by phplist. But the first option works fine.
- proceed as a normal import :
- select destination list(s)
- set file to be imported
- in field delimiter, type a comma (,) if your csv file is comma separated. If you have used the excel macro shown above, this will be the case. If your csv is from another source, you may have ; as a delimiter for example. Just type the proper caracter there.
- do enter anything as Record delimiter
- set other parameters as you see fit
- Select send notification email or Make confirmed immediatly as you see fit
- Hit the import button
It works for me !!