Author Topic: Import address book from csv file  (Read 22225 times)

Offline namit

  • Jr. Member
  • **
  • Posts: 11
Import address book from csv file
« on: October 19, 2006, 02:55:52 PM »
Pass down the name of the file you want
And the users id that is in the sql table

csv layout
Name, Email

Hope it helps ya all.

Code: [Select]
<?php

$dbhost 
'localhost';
$dbuser 'root';
$dbpass 'password';
$conn mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
$dbname 'roundcubemail';

mysql_select_db($dbname$conn);
echo 
mysql_errno($conn) . &quot;: &quot; . mysql_error($conn). &quot;\n&quot;;

///Fin connection

$user_id $_GET['user_id'];
$file $_GET['file'];

if(
$user_id == &quot;&quot; || $file == &quot;&quot;)
{
echo &quot;You have no user id $user_id<br />OR files&quot;;
}
if(
$user_id != &quot;&quot; || $file != &quot;&quot;)
{

echo 
$user_id;
echo &
quot;<br />$file&quot;;

$row 1;
$handle fopen($file, &quot;r&quot;);
echo &
quot;$handle&quot;;

while ((
$data fgetcsv($handle1000, &quot;,&quot;)) !== FALSE)
{

echo &quot;<br />&quot;;

$row++;

$name $data[0];
$email $data[1];

echo &quot;<b>Name: </b>&quot; . $name. &quot;\n&quot;;
echo &quot;<B>Email:</B> &quot; . $email . &quot;\n&quot;;

$query = &quot;INSERT INTO `contacts` ( `contact_id` , `user_id` , `changed` , `del` , `name` , `email` , `firstname` , `surname` , `vcard` ) VALUES '''$user_id''1985-09-15 00:00:00''0''$name''$email''''''' );&quot;;
echo &quot;<br />&quot;;

mysql_query($query$conn);
echo mysql_errno($conn) . &quot;: &quot; . mysql_error($conn) . &quot;\n&quot;;

}

fclose($handle);

}


?>

Offline CReber

  • Newbie
  • *
  • Posts: 5
Re: Import address book from csv file
« Reply #1 on: October 30, 2006, 04:14:26 PM »
Can you give me a sample of the csv file? Don't wanna crash the db ;)

Thanks!

Offline namit

  • Jr. Member
  • **
  • Posts: 11
Re: Import address book from csv file
« Reply #2 on: October 30, 2006, 05:46:39 PM »
csv layout

Name, Email

Offline Nonsense

  • Jr. Member
  • **
  • Posts: 17
Re: Import address book from csv file
« Reply #3 on: October 30, 2006, 06:16:48 PM »
May seem like a stupid question, but where exactly (in which lines) do I insert username and file?

Offline namit

  • Jr. Member
  • **
  • Posts: 11
Re: Import address book from csv file
« Reply #4 on: October 30, 2006, 07:25:03 PM »
well you will have on your webserver the csv file then you will have

http://yourwebsite.com/cvsupload.php?user_id=THE_USER_ID_THAT_U_HAVE_IN_SQL_TABLE&file=FILE_NAME

Offline CReber

  • Newbie
  • *
  • Posts: 5
Re: Import address book from csv file
« Reply #5 on: October 30, 2006, 08:57:52 PM »
link doesn't work :P just quote an exact sample of a csv file you would recommend! only 3 lines...

thanks anyway....

Offline namit

  • Jr. Member
  • **
  • Posts: 11
Re: Import address book from csv file
« Reply #6 on: October 31, 2006, 03:12:15 AM »

Create a directory on your server called cvsupload.php
upload the cvs file to your server
format is
bob,boobs@boobs.com
chris noen,namit@something.org

create a file called cvsupload.php in the direcotry csv-upload.

find in the sql tables the userid within the roundcube tables then replaec that iin the link below.

replace yourwebsite.com with your website
http://yourwebsite.com/cvsupload.php?user_id=THE_USER_ID_THAT_U_HAVE_IN_SQL_TABLE&file=FILE_NAME

Offline Nonsense

  • Jr. Member
  • **
  • Posts: 17
Re: Import address book from csv file
« Reply #7 on: October 31, 2006, 03:37:14 AM »
That didn't work.

Code: [Select]
0: user_id (my emailaddress)
file.csvResource id #3
Name: Name Name Email: email
0:
Name: Name Name Email: email
0:

Offline CReber

  • Newbie
  • *
  • Posts: 5
Re: Import address book from csv file
« Reply #8 on: October 31, 2006, 06:45:37 AM »
same like me! the script only imports the first 3 contacts and then it stops...

Code: [Select]
0: 1
contacts.csvResource id #3
Name: Berlin - Michael Email: [email]michael@berlin.de[/email]
0:
Name: Dublin - Billy Email: [email]billy@dublin.com[/email]
0:
Name: Dublin - William Email: [email]william@dublin.com[/email]


The output log seems ok, so what else can that be?

Offline namit

  • Jr. Member
  • **
  • Posts: 11
Re: Import address book from csv file
« Reply #9 on: October 31, 2006, 07:19:45 AM »
Can you check the sql tables and see if the are being entered?

You also have to logout and then login to roundcube.

Offline Nonsense

  • Jr. Member
  • **
  • Posts: 17
Re: Import address book from csv file
« Reply #10 on: October 31, 2006, 09:13:07 AM »
Solved.
It was a typo.

Thanks.

Offline namit

  • Jr. Member
  • **
  • Posts: 11
Re: Import address book from csv file
« Reply #11 on: October 31, 2006, 09:27:52 AM »
in my code or your code? if it was my code give it to me and i will change my code.

Offline Nonsense

  • Jr. Member
  • **
  • Posts: 17
Re: Import address book from csv file
« Reply #12 on: October 31, 2006, 09:52:44 AM »
My code. So don't worry.

Offline rantdepot

  • Newbie
  • *
  • Posts: 5
Re: Import address book from csv file
« Reply #13 on: November 06, 2006, 04:00:24 PM »
Worked well, thanks! 8)

Offline klemen

  • Newbie
  • *
  • Posts: 3
Re: Import address book from csv file
« Reply #14 on: May 16, 2007, 03:06:22 AM »
I have an csv file format:

firstname,surname,name,,name@email.com,,,,,,,,,,,,,,,,,,,,,,,

I get surname where I need email. How to edit the script to get right insert.

thanks.