MySQL Forums
Forum List  »  PHP

Re: How insert data in table using a .txt file
Posted by: Julia Fleur
Date: April 06, 2010 04:34PM

The script works if I already data.txt pre-uploaded onto the server.

Now I'm having problems with uploading any .txt file (using a form) so that it can run through the script and load into 'data'

Here's what i have thus far:

<form method="post" action="process_upload.php" enctype="multipart/form-data">
<input type="file" name="userfile" value="Locate File" /><br /><br />
<input name="upload" type="submit" value="Submit" />
</form>
</p>



<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$objFile=new exportFile;
$objFile->connect();
$objFile->exportFileToDatbase("$fileName","~","r","data",8);
// File name,Seprator,mode,tablename,field
?>


process_upload.php :

<?php


class exportFile
{
var $Query_ID=0;
var $connection=0;

function connect()
{
if($this->connection==0) {
$this->connection=;
$SelectResult =;
} else {
echo "Connection Couldnot be Established";
die();
}
}

function query($sql) {
$this->Query_ID=mysql_query($sql,$this->connection);
if(!$this->Query_ID) {
echo "Query Failed".mysql_error();
} else {
return $this->Query_ID;
}
}

function exportFileToDatbase($filename,$de,$mode,$tablename,$fieldno)
{

$fd=fopen($filename,"$mode");
while(!feof($fd)) {
$line=fgets($fd,5000);
$f=explode($de,$line);
for($i=0;$i<$fieldno;$i++) {
$a[]=trim("'$f[$i]'");
}
$value=implode(",",$a);
unset($a);
$sql="insert into $tablename values($value)";
//echo $sql;
$this->query($sql);

}

}

}
?>



Edited 1 time(s). Last edit at 04/06/2010 04:35PM by Julia Fleur.

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.