file open, add mysql database for each section

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

file open, add mysql database for each section

Post by phice »

ok, so I've got a file with hundreds of little sections, much like the following:
File:3D_Ultra_Cool_POOL.EXE
Length:156005594 Bytes,152349KB
UUHash:=myTDhYcJg4v03KDQPl7fpOXidFI=
For File, I want that to go into a mysql database table, under column FILE. Length under LENGTH, UUHash under UUHASH. And all in one row. And each column would not include anything before the :, nor the :.

Any ideas on how to do such a thing? Need more information?
Image Image
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Are all of the sections devided by a blank line or a special character?
Is the file a .txt file?
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Here's a portion of the .txt file:
File:3D_Ultra_Cool_POOL.EXE
Length:156005594 Bytes,152349KB
UUHash:=myTDhYcJg4v03KDQPl7fpOXidFI=

File:AQUANOX.EXE
Length:185422848 Bytes,181077KB
UUHash:=pbGrzFVasD3jHKFvh1Zv5BI4c2Q=

File:BATTLE_REALMS.EXE
Length:151040742 Bytes,147501KB
UUHash:=3vGJRrsGtOMa2w89+HLXFPSv+5Q=

File:BLADE_OF_DARKNESS.EXE
Length:195208527 Bytes,190633KB
UUHash:=foI6DUrhotl1UWzihRkcIQoyB4Y=

File:C_C_RENEGADE.EXE
Length:187781360 Bytes,183380KB
UUHash:=2C38tOa+ygkZjeh+WENUD9QlVfc=
Each group is seperated by two \n.
Image Image
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Okey.

Well.. as long as the only place you have \n\n is between each section then it should be pretty easy to do.. I think.

The first thing that I would do if I was trying to do the same thing would be to load the entire text file into a variable named $thefile.

Then once you have it use the explode() function to split that variable into chunks like this... $chunks=explode("\n\n",$thefile);

Now you will have an array called $chunks which contains each section.

From here you could do some like this..

Code: Select all

<?php

$number_of_chunks=count($chunks); //get the number of chunks

$loop=0;

while($loop<=$number_of_chunks)
{

$tinychunk=explode("\n",$chunks[$loop]);

$tinychunk[0]=str_replace("File:","",$tinychunk[0]);
$tinychunk[1]=str_replace("Length:","",$tinychunk[1]);
$tinychunk[2]=str_replace("UUHash:","",$tinychunk[2]);

mysql_query("INSERT INTO `your_table` (`file`,`length`,`uuhash`) VALUES ('$tinychunk[0]','$tinychunk[1]','$tinychunk[2]')";

$loop++;
}

?>
If everything works it should split up your file into bits and then add each bit into your database as you want.

You will obviously need to add your database connection code in their.. and you may need to tweek the code a bit but it should give you a rough idea of what to do.


Hope this helps.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Worked perfect. 8)
Image Image
Post Reply