Re: Uploaded CSV -> database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Amazing - it works! The uploaded file is always stored in the temp directory, so that issue was kind of moot. Here's my complete code, if anyone else has this same need:

if(isset($_FILES['userfile']['tmp_name'])) {
    $csvfile = fopen($_FILES['userfile']['tmp_name'], "r");
    if($csvfile == FALSE) die('error opening file');
    while(($datarow = fgetcsv($csvfile, 1000)) !== FALSE) {
        $name  = isset($datarow[0]) ? $datarow[0] : '';
        $addr  = isset($datarow[1]) ? $datarow[1] : '';
        $city  = isset($datarow[4]) ? $datarow[4] : '';
        $state = isset($datarow[5]) ? $datarow[5] : '';
        $zip   = isset($datarow[6]) ? $datarow[6] : '';
$query = "insert into dealers (account_id,name,addr,city,state) values ('$account_id','$name','$addr','$city','$state','$zip');";
        $result = mysql_query($query) or die(mysql_error());
    }
    fclose($csvfile);
}

I was really surprised at how short & simple this turned out to be. There is plenty of room for error-checking in the above snippet, but what's there works when there are no problems.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux