Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Parsing File, Inserting into DB
Old 03-11-2008, 07:39 PM Parsing File, Inserting into DB
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
 
Register now for full access!
Old 03-12-2008, 12:42 AM Re: Parsing File, Inserting into DB
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
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
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-12-2008, 11:49 PM Re: Parsing File, Inserting into DB
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
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_path0777);
} 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?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 03-13-2008, 02:54 AM Re: Parsing File, Inserting into DB
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
What error do you get? Do you add form enctype="multipart/post-data"?
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 03-13-2008, 08:56 AM Re: Parsing File, Inserting into DB
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
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?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!


Last edited by Galaxian; 03-13-2008 at 10:01 AM..
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 03-13-2008, 10:39 AM Re: Parsing File, Inserting into DB
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!


Last edited by Galaxian; 03-13-2008 at 10:42 AM..
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 03-13-2008, 11:48 AM Re: Parsing File, Inserting into DB
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
This might help:

http://regexlib.com/CheatSheet.aspx
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 03-13-2008, 01:25 PM Re: Parsing File, Inserting into DB
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
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..
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 03-13-2008, 02:07 PM Re: Parsing File, Inserting into DB
VirtuosiMedia's Avatar
Web Design Made Simple

Posts: 1,228
Trades: 0
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:

PHP Code:
'^[0-9]{3,}$' 
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..
VirtuosiMedia is offline
Reply With Quote
View Public Profile Visit VirtuosiMedia's homepage!
 
Old 03-14-2008, 01:38 AM Re: Parsing File, Inserting into DB
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 03-14-2008, 05:50 AM Re: Parsing File, Inserting into DB
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
Quote:
Originally Posted by mtishetsky View Post
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.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Reply     « Reply to Parsing File, Inserting into DB
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.66091 seconds with 12 queries