thanks but that did not offer much aid. I am attempting to use the browse to get a file and then before placing it anywhere I want to parse it and put it into a table. I am trying to get the script to work, but am having trouble. I am working off this scripts example but instead of grabing a file off the server i want to grab it and parse it before hand.
I am just not sure on how to use the
$fcontents = isset($POST['upload']) && $FILES['userfile']['size'] > 0)
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
<?
first get a mysql connection as per the FAQ
$fcontents = file ('./spreadsheet.xls');
expects the csv file to be in the same dir as this script
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode("\t", $line);
#if your data is comma separated
instead of tab separated,
change the '\t' above to ','
$sql = "insert into TABLENAME values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
?>
here is what i have writen -------------------------------
<?
first get a mysql connection as per the FAQ
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$fcontents = $_FILES['userfile']['tmp_name'];
# expects the csv file to be in the same dir as this script
$fp = fopen($fcontents, 'r');
$content = fread($fp, filesize($tmpName));
for($i=0; $i<sizeof($fp); $i++)
{
$line = trim($fp[$i]);
$arr = explode("\t", $line);
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ','
// Connects to the dba
$hostname_mydba = "localhost";
$database_mydba = "ResultsDB";
$username_mydba = "root";
$password_mydba = "pass";
$mydba = mysql_connect($hostname_mydba, $username_mydba, $password_mydba) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_mydba);
$query = "INSERT INTO 2004Scores (Bib, firstName, lastName, Address, City, State, Zip, Email, FinTimeOfDay ) ".
"VALUES ('". implode("','", $arr) ."')";
mysql_query($query) or die('Error, query failed');
mysql_close($mydba);
echo "<br>File $fileName uploaded<br>";
}
}
?>