Hi everyone,

I have a text file with similar content as below:

admin,123
user,123
user2,123

What i need to perform is to upload the text file and read each lines. Then split the content of each lines by delimiter and add it to database. In my database there are two fields; user and password.


Any PHP Guru, Please guide me on this issue.

Thank in advance.

This is the code that i have used to split the uploaded data by lines.

<?
 if (isset($_POST['Submit'])){
 
	$fd = fopen ($form_data, "r"); 
	$contents = fread ($fd,filesize ($form_data));

	fclose ($fd); 
	$delimiter = ",";
	$splitcontents = explode($delimiter, $contents);
	$counter = "";
 
 	 
	$lines = file($form_data);
	foreach($lines as $line_num => $line)
	{
	echo $line;
	echo "<br>";

	
	}
 }
 ?>

 <form method="post" action="" enctype="multipart/form-data"> 

 <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
 <br>File to upload:<br> 
 <input type="file" name="form_data" size="40"> 
 <p><input type="submit" name="Submit" value="Submit"> 
 </form>

Try this:

<?
if (isset($_POST['Submit'])){
 
	$fd = fopen ($form_data, "r");

	while(!feof($fd)) {
		$line = fgets($fd);
		
		list($data[$counter]->name, $data[$counter]->pass) = explode(',', $line);
		$counter++;
	}

	fclose ($fd);

	foreach($data as $user) {
		$sql = "INSERT INTO `table_name` (`field_1`, `field_2`)
				VALUES('$user->name', '$user->pass')";
		mysql_query($sql) or die('Cannot insert the data. Error: ' . mysql_error());
	}
}
?>
 
<form method="post" action="" enctype="multipart/form-data">
 
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload:<br>
<input type="file" name="form_data" size="40">
<p><input type="submit" name="Submit" value="Submit">
</form>

Not tested ! Post again if there is still problem with the above codes.

Member Avatar for diafol

Personally, I'd split twice - 1st on '\r\n' to give lines, 2nd on ',' to give items.
Then instead of executing an SQL query on EVERY iteration, I'd build a VALUES clause and just execute that:

$str = file_get_contents($file);
$lines = explode("\r\n",$str);
foreach($lines as $line){
  $items = explode(",",$line);
  $values[] = "('{$items[0]}','{$items[1]}')";
}
$valstring = implode(",",$values[]);
$rs = mysql_query("INSERT INTO `table_name` (`field_1`, `field_2`)
				VALUES($valstring)";

Not tested.

I'm trying to create a pdf form using php & Mysql for a school. i have the db query right. but when I exract the file, I receive the home address in one line. I don't want it in one line, but in three lines.

E.g: 254 westend drive sommerset west 0125.

I want it like

254 westend drive
sommerset west
0125


this is were it picks up the information.

$data [++$counter] = $row ["parent1_home_address"];
$border[$counter] = "LR";

Member Avatar for diafol

You need to provide a delimiter in the full address that's stored in the DB. Otherwise your script will be guessing where to put the newline. Usually this would be a ',', but of course some addresses may show:

254, westend drive, sommerset west, 0125
and some
254 westend drive, sommerset west, 0125

My suggestion would be a replace on ',' in this case.

$new_address = str_replace(",","<br />",$address);

that may however give you:

254
westend drive
sommerset west
0125

You could think up a regular expression for the replacement maybe to leave the first number or number-text combo with comma to not be replaced using preg_replace(). I can't think of one sorry.

Try this:

<?
if (isset($_POST['Submit'])){
 
	$fd = fopen ($form_data, "r");

	while(!feof($fd)) {
		$line = fgets($fd);
		
		list($data[$counter]->name, $data[$counter]->pass) = explode(',', $line);
		$counter++;
	}

	fclose ($fd);

	foreach($data as $user) {
		$sql = "INSERT INTO `table_name` (`field_1`, `field_2`)
				VALUES('$user->name', '$user->pass')";
		mysql_query($sql) or die('Cannot insert the data. Error: ' . mysql_error());
	}
}
?>
 
<form method="post" action="" enctype="multipart/form-data">
 
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<br>File to upload:<br>
<input type="file" name="form_data" size="40">
<p><input type="submit" name="Submit" value="Submit">
</form>

Not tested ! Post again if there is still problem with the above codes.

I tested the code and its giving following error:

Notice: Undefined variable: counter in C:\xampp\htdocs\halal\upload3.php on line 11

Strict Standards: Creating default object from empty value in C:\xampp\htdocs\halal\upload3.php on line 11

Notice: Undefined variable: counter in C:\xampp\htdocs\halal\upload3.php on line 11

Notice: Undefined variable: counter in C:\xampp\htdocs\halal\upload3.php on line 12

Strict Standards: Creating default object from empty value in C:\xampp\htdocs\halal\upload3.php on line 11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.