Re: Uploaded CSV -> database
| From: | Brian Dunning | Date: | Tue, 18 Oct 2005 13:15:00 +0000 |
| Subject: | Re: Uploaded CSV -> database | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-224273@lists.php.net to get a copy of this message | ||
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.