Parsing File, Inserting into DB
03-11-2008, 07:39 PM
|
Parsing File, Inserting into DB
|
Posts: 842
Name: Rich Powell
Location: United Kingdom
|
I am starting to sell some serials for an application which I buy Wholesale and to save some time and hassle, I'd like to automate the insertion of the serials into the database.
They come in files similar to the following:
Code:
=============================serials=================================
num carduser cardpass
0001: 000000e10000000 A1BCDE23F4GH56I7
0002: 000000e10000000 A1BCDE23F4GH56I7
0003: 000000e10000000 A1BCDE23F4GH56I7
==================================================================
So what I need is for it to get the carduser and cardpass and insert it into the database.
Also, at the 8th character (in this example a 1) it will define the type of serial it is.
Any assistance?  Thank you.
|
|
|
|
03-12-2008, 12:42 AM
|
Re: Parsing File, Inserting into DB
|
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
|
Assuming that it is tab delimited, you can use a combination of file data array, array spliting and regex
PHP Code:
$file = file('filename.txt'); for ($i = 0; $i < count($file); $i++) { if (preg_match('/^[0-9]+:\\t[0-9]+[a-z][0-9]{2,}\\t[A-Z0-9]+$/', $file[$i])) { $dataline = explode("\t", $file[$i]); $carduser = $dataline[1]; $cardpass = $dataline[2]; preg_match('/^[0-9]+[a-z]([0-9])[0-9]+$/', $carduser, $match); $serial_type = $match[1]; } }
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
Last edited by mgraphic; 03-12-2008 at 12:50 AM..
Reason: silly error
|
|
|
|
03-12-2008, 11:49 PM
|
Re: Parsing File, Inserting into DB
|
Posts: 842
Name: Rich Powell
Location: United Kingdom
|
Thank you
It appears I am having problems reading uploaded files. Though it managed to read correctly a file in the same directory that I uploaded manually.
So I tried setting $file as $_FILES['codes']['tmp_name'] at first and no luck, so tried moving the file to a new folder called tmp in the same directory. Still not reading it. Here's the code:
PHP Code:
if(isset($_POST['submit'])){
$target_path = "tmp/";
$target_path = $target_path . basename( $_FILES['codes']['name']);
if(move_uploaded_file($_FILES['codes']['tmp_name'], $target_path)) { chmod($target_path, 0777); } else { die(); }
$file = file($target_path); $count = 1; for ($i = 0; $i < count($file); $i++) { if (preg_match('/^[0-9]+:\\t[0-9]+[a-z][0-9]{2,}\\t[A-Z0-9]+$/', $file[$i])) { $dataline = explode("\t", $file[$i]); $carduser = $dataline[1]; $cardpass = $dataline[2]; preg_match('/^[0-9]+[a-z]([0-9])[0-9]+$/', $carduser, $match); $serial_type = $match[1];
print ("$serial_type $carduser $cardpass\n<br />"); /* if(@mysql_query("insert into serial_codes values ('', $serial_type, $card_user, $card_pass, NOW(), '', '')")){ $success = 1; } else { $success = 0; }*/ $count ++; } }
}
Any suggestions?
|
|
|
|
03-13-2008, 02:54 AM
|
Re: Parsing File, Inserting into DB
|
Posts: 1,226
Name: Mike
Location: Mataro, Spain
|
What error do you get? Do you add form enctype="multipart/post-data"?
|
|
|
|
03-13-2008, 08:56 AM
|
Re: Parsing File, Inserting into DB
|
Posts: 842
Name: Rich Powell
Location: United Kingdom
|
The file uploads fine into the directory with the specified chmod('',0777) and the form is indeed multipart/form-data.
Put simply, I am not sure what the error is. It doesn't seem to pickup the file if I'm working with the upload.
After some study
It works if it is manually uploaded and set to:
PHP Code:
$file = file('serials1.txt');
It will not work if it is uploaded through the form and is
PHP Code:
$file = file(basename($_FILES['codes']['name']));
The only thing I could think of that is actually different here is the chmod, which is set to 0777 through PHP correctly. I am really confused now.
Edit Update:
Ok... After playing with the code it seems to read the $file = file() array because I test printed $file[2] and wo and behold it was there..
I suspect the violating code is:
PHP Code:
if (preg_match('/^[0-9]+:\\t[0-9]+[a-z][0-9]{2,}\\t[A-Z0-9]+$/', $file[$i])){
somewhere in the opening there
I suspect that preg_match.. I'm not too up to shape with my regular expressions but I'll reiterate the file it is reading is tabulated and in this format:
Code:
=============================serials=================================
num carduser cardpass
0001: 000000e10000000 A1BCDE23F4GH56I7
0002: 000000e10000000 A1BCDE23F4GH56I7
0003: 000000e10000000 A1BCDE23F4GH56I7
==================================================================
And it is indeed tabulated at the spaces there (unfortunately the vb editor won't replicate the tabs so I inserted them manually.)
Any suggestions?
Last edited by Galaxian; 03-13-2008 at 10:01 AM..
|
|
|
|
03-13-2008, 10:39 AM
|
Re: Parsing File, Inserting into DB
|
Posts: 842
Name: Rich Powell
Location: United Kingdom
|
Well definitely this has me at a loss and I am left very frustrated.. I don't understand it. What is the difference with the files as to why the preg_match isn't working?
Edit: what the .. it appears to work if I comment out the preg_match .. however I don't want to keep this because I'd like it to keep a good filter on serials and excessive lines in the txt file.
Last edited by Galaxian; 03-13-2008 at 10:42 AM..
|
|
|
|
03-13-2008, 01:25 PM
|
Re: Parsing File, Inserting into DB
|
Posts: 842
Name: Rich Powell
Location: United Kingdom
|
Still no clue. Guess the preg_match will have to be left commented out. I've done a basic check so it checks the userpass is there so all is good for now I guess..
|
|
|
|
03-13-2008, 02:07 PM
|
Re: Parsing File, Inserting into DB
|
Posts: 1,228
|
PHP Code:
'/^[0-9]+:\\t[0-9]+[a-z][0-9]{2,}\\t[A-Z0-9]+$/'
This code is also checking the num column, with the colon and the tabs included. I'm not sure if that's what you want.
I haven't tested these, but assuming the examples you provided are the format, try the following regexs (all are case insensitive):
To check the number as at least three digits, use:
To check the carduser, use:
PHP Code:
'^[0-9]{6}[a-zA-Z]{1}[0-9]{7}$'
To check the cardpass, use:
PHP Code:
'^[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{4}[0-9]{2}[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{2}[0-9]{2}[a-zA-Z]{1}[0-9]{1}$'
If you want them all combined, with the tabs and the colon, try this:
PHP Code:
'^[0-9]{3,}:\t[0-9]{6}[a-zA-Z]{1}[0-9]{7}\t[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{4}[0-9]{2}[a-zA-Z]{1}[0-9]{1}[a-zA-Z]{2}[0-9]{2}[a-zA-Z]{1}[0-9]{1}$'
You might be having trouble because of the tabs, though. If they're spaces, you'll have to check for those instead.
Last edited by VirtuosiMedia; 03-13-2008 at 02:22 PM..
|
|
|
|
03-14-2008, 01:38 AM
|
Re: Parsing File, Inserting into DB
|
Posts: 1,226
Name: Mike
Location: Mataro, Spain
|
PHP Code:
$file = file(basename($_FILES['codes']['name']));
is wrong. Actually your uploaded file is stored on your server as something like /var/tmp/phpYGHIhj which is ['tmp_name'], not the ['name']. ['name'] is the original file name as it was on client's computer.
|
|
|
|
03-14-2008, 05:50 AM
|
Re: Parsing File, Inserting into DB
|
Posts: 842
Name: Rich Powell
Location: United Kingdom
|
Quote:
Originally Posted by mtishetsky
PHP Code:
$file = file(basename($_FILES['codes']['name']));
is wrong. Actually your uploaded file is stored on your server as something like /var/tmp/phpYGHIhj which is ['tmp_name'], not the ['name']. ['name'] is the original file name as it was on client's computer.
|
Apologies, I didn't include the code that handled this because that part worked.
|
|
|
|
|
« Reply to Parsing File, Inserting into DB
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|