FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Uploading Files to the Database





fizzo
hi good day everyone I have some problems that I think you guys can help me. I have a File its a .txt file Text file from notepad and here is the .txt file im telling you.. STATS FILE

As you can see its a text file that has Stats of players from a NBA Live Game. I would want to tell if how can I upload this files and save it on the database

For Example I have a Field called Names where the player names will be saved and POS as position of the player and MIN as minutes played by the players and so on... My problem will be on how can I upload the file to the database and save it? I hope someone from you can help me Thank you very Much Have a nice day once again.
kv
I can't write the entire code for your problem, but here are few steps.

write a html page with a form containing an input box of type file and submit button

Code:

<form action="save.php">
<input name="statfile" type="file">
<input type="submit">
</form>


In save.php, get the uploaded file contents into a variable using http://fr.php.net/manual/en/function.move-uploaded-file.php and http://fr.php.net/manual/en/function.file-get-contents.php.

Create a table in mysql with a blob field. Use insert statement to insert the file data into the blob field.

If you are using frihost, make sure it allows saving file into database.
hexkid
fizzo wrote:
My problem will be on how can I upload the file to the database and save it?
You upload it to the server, not the database.
http://www.php.net/manual/en/features.file-upload.php

After you have the file, read it line by line
http://php.net/fopen, http://php.net/fgets, http://php.net/fclose (or http://php.net/file to read it into an array)

Now for each line, parse it, validate and write to the database.

Code:
<?php
// ...

$line = 'S. Jackson        G       34      16-26   4-8     0-1     1       2       5       1       1       2       3       36     
';
$elements = preg_split('/\s+/', $line); // very basic parse
// $elements[0] == 'S.'
// $elements[1] == 'Jackson'
// $elements[2] == 'G'
// ...

// ...
?>
kv
hexkid wrote:
fizzo wrote:
My problem will be on how can I upload the file to the database and save it?
You upload it to the server, not the database.
http://www.php.net/manual/en/features.file-upload.php

After you have the file, read it line by line
http://php.net/fopen, http://php.net/fgets, http://php.net/fclose (or http://php.net/file to read it into an array)

Now for each line, parse it, validate and write to the database.

Code:
<?php
// ...

$line = 'S. Jackson        G       34      16-26   4-8     0-1     1       2       5       1       1       2       3       36     
';
$elements = preg_split('/\s+/', $line); // very basic parse
// $elements[0] == 'S.'
// $elements[1] == 'Jackson'
// $elements[2] == 'G'
// ...

// ...
?>


Not necessary to parse each line. You can get contents of entire file using [url="http://fr.php.net/manual/en/function.file-get-contents.php"]file-get-contents[/url]
hexkid
kv wrote:
Not necessary to parse each line.
I beg to disagree.
What will you do with the result of file_get_contents()?

I bet you thought of splitting it on "\n" and parse each element of the resulting array. Well ... each element of the resulting array is a line!


I know that won't happen in this case, but imagine that the file size is 42Gb. Can you do a file_get_contents() on it? And notice no performance decrease? I doubt that very very much.
My suggestion is to stick with reading it line by line even for a 2Kb file.


Anyway I also pointed to file(), which is just about the same as file_get_contents(), but has the added benefit that lines are already separated into different elements. In respect to file size it suffers from the same problems as file_get_contents().
fizzo
Hexkid would you want to help me build a script coz Im new with PHP Scripting. I need more help.
hexkid
fizzo wrote:
Hexkid would you want to help me build a script coz Im new with PHP Scripting. I need more help.

Post your questions to the forum. I'll gladly help you.
fizzo
Can you help me on a Step by Step procedure on how to make a script like this. Thanks!
hexkid
fizzo wrote:
Can you help me on a Step by Step procedure on how to make a script like this. Thanks!

Start by reading the links in one of my previous messages in this topic.

Also the introductory tutorial on the PHP website might prove useful.
fizzo
Parsing Fixed-Width Field Data Record

Code:
<?php
$fp = fopen('fixed-width-records.txt','r') or die ("can't open file");
while ($s = fgets($fp,1024)) {
    $fields[1] = substr($s,0,10);  // first field:  first 10 characters of the line
    $fields[2] = substr($s,10,5);  // second field: next 5 characters of the line
    $fields[3] = substr($s,15,12); // third field:  next 12 characters of the line
    // a function to do something with the fields
    process_fields($fields);
}
fclose($fp) or die("can't close file");
?>


Hexkid is this a the way how to parse? can this be ok for what i need on reading each line of the TEXT File?
hexkid
Yep!

I have two suggestions:
a) Use "b" in the mode parameter for fopen(); it has absolutely no effect on Linux and removes some magic on Windows. I don't like magic things to happen. I haven't tested it with Mac's or other Operating Systems.
Code:
fopen('fixed-width-records.txt', 'rb')


b) What happened to $fields[0]? At first it may look strange to start counting at zero, but it makes loops and tests much simpler. Try to always use the 0 index of arrays, you'll get used to it -- and you'll understand faster any code by somebody else you come across -- everybody uses 0 Smile
Code:
$fields[0] = substr($s, 0, 10);
fizzo
hexkid wrote:

Code:
fopen('fixed-width-records.txt', 'rb')



So instead of 'r' only it will be 'rb'? Thanks dude! Then after the parsing thing.. I can add the codes to upload it on the database
Related topics
uploading files through web browser
Uploading Files
A very good PHP MySQL Tutorial
Issue uploading SQL database.
Uploading files error..
uploading files
Uploading and resizing an image
Uploading Files..???
Anyone can suggest a good webhost? read on...
Trying to start out!!!
Uploading and unpacking Compressed Files
Uploading files
Is there any restrict MB of uploading database file?
create new database privilege
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.