0

Possible Duplicate:
PHP CSV upload files

i have a csv file ,te file has fields like uname,password,fistname,lname,email. My requirement is when i upload a csv ,it should check for duplicate username and email also it should chekc whether the uname and password values exists in the database. Basically it has to remove all the duplicates and upload How can i achieve it ?

RONIE

Community
  • 1
  • 1
ronnie
  • 1

3 Answers3

4
function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $data;
}

CSV like so

"usename","password"     #Header

"robert 1", you_wish
"robert 1", you_wish

Output like so:

Array
(
    [0] => Array
        (
            [username] => robert 1
            [password] => you_wish
        )

    [1] => Array
        (
            [username] => robert 2
            [password] => you_wish
        )

)

Reference: http://gist.github.com/385876

RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • got duplicate checking frm http://stackoverflow.com/questions/1139571/remove-dupes-sort-from-a-array-of-associative-arrays-in-php – ronnie Sep 10 '10 at 22:25
2

A simple way to do this would be to place UNIQUE constraints on the username and email fields in your database, and look for a violation of the constraint when you try to insert a new (username, password, firstname, lastname, email) tuple. If you've desginated one of these fields as the primary key, that's already got a unique constraint on it.

Also, I hope that you're not actually storing passwords directly in a database...?

ngroot
  • 1,186
  • 5
  • 11
0

You will most likely need to do this in a few steps.

  1. Receive the file through the upload form (or whatever method)

  2. Read the file in (you can probably just use the tmp location that it gets uploaded to, or give it a temporary name)

  3. Parse each line and check against the database

  4. Write all the valid lines into the permanent location

ashurexm
  • 6,209
  • 3
  • 45
  • 69
  • I got the array Array ( [0] => Array ( [username] => uname1 [fname] => fname1 [lname] => lname1 [email] => email1@email.com) [1] => Array ( [username] => uname2 [fname] => fname2 [lname] => lname2 [email] => email2@email.com) [2] => Array ( [username] => uname1 [fname] => fname1 [lname] => lname1 [email] => email1@email.com) [3] => Array ( [username] => uname3 [fname] => fname3 [lname] => lname3 [email] => email3@email.com) ) removing the array 2 since uname and emails are the same as in array0 ?????? – ronnie Sep 10 '10 at 20:51
  • I got the array Array ( [0] => Array ( [username] => uname1 [fname] => fname1 [lname] => lname1 [email] => email1@email.com) [1] => Array ( [username] => uname2 [fname] => fname2 [lname] => lname2 [email] => email2@email.com) [2] => Array ( [username] => uname1 [fname] => fname1 [lname] => lname1 [email] => email1@email.com) [3] => Array ( [username] => uname3 [fname] => fname3 [lname] => lname3 [email] => email3@email.com) ) removing the array 2 since uname and emails are the same as in array0 ?????? – ronnie Sep 10 '10 at 20:54