I am wanting to select a file through the browse. TO then place that text file through a function that will parse it and place it into the table that I have selected through the form. However I am not able to get it to work does anyone know why? I have the code and functions and form below.
<?php
if(isset($POST['upload']) && $FILES['userfile']['size'] > 0)
{
$fileName = $FILES['userfile']['name'];
$tmpName = $FILES['userfile']['tmp_name'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
$fcontents = file ($fileName) or die("could not get file");
# expects the csv file to be in the same dir as this script
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
// echo $line;
$arr = explode("\t", $line);
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ','
$sql = "INSERT INTO 2004Results (Bib, firstName, lastName, Address, City, State, Zip, Email, FinTimeOfDay ) ".
"VALUES ('". implode("','", $arr) ."')";
mysql_query($query) or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";
}
fclose($fp);
?>
<?php
if($submit!="" && $userfile != "" && $txtId2 != "" && $userfile == "0" && $chooseTable == "0"){
doInsert($chooseFile);
print("Data inserted.");
}
else
print("Data not inserted.");
?>
<form action="<?php echo($PHP_SELF) ?>" >
<table class="formBg" align="center" width="42%" border="1" cellspacing="0" cellpadding="2">
<tr valign="top">
<td width="43%">Choose file here</td>
<td width="57%">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
</tr>
<tr valign="top">
<td width="43%">Choose table here</td>
<td width="57%">
<select class="dropdownMedium" name="chooseTable">
<option selected value="0">--- Choose table here ---</option>
<?php getTableName(); ?>
</select>
<input class="medium" type="hidden" name="txtId2" value="<?php print(unFormatData($row[1])); ?>">
</td>
</tr>
<tr valign="top">
<td width="43%"> </td>
<td width="57%">
<input name="upload" type="submit" class="box" id="upload" value=" Upload ">
<input class="button" type="reset" name="reset" value="Reset">
</td>
</tr>
</table>
</form>
function doInsert($txtFile){
Global $connection;
$txtFile = formatData($txtFile);
hookUpDb2();
for($i=0; $i<sizeof($txtFile); $i++) {
$line = trim($txtFile[$i]);
// echo $line;
$arr = explode("\t", $line);
$query = "INSERT INTO 2004Scores (Bib, firstName, lastName, Address, City, State, Zip, Email, FinTimeOfDay ) ".
"VALUES ('". implode("','", $arr) ."')";
}
mysql_query($query,$connection);
mysql_close($connection);
}