|
Posted by J.O. Aho on 01/30/06 20:50
gerg wrote:
> This is a wealth of info. Thanks so much for your response. If I may
> ponder a few more ideas:
>
> so, i'll upload the file ( I can write a scipt or find one to use )
>
> Open said file with fopen() ( Where does fgets() come into play. Seems
> to only return a lenght? I wasn't getting much from php.net. )
When using fopen(), it don't read the file itself, you need to loop through
the whole file in a similar way as when fetching rows from the database (see
my previous post) and you will read one line at the time.
You can choose to process the rows at the same time you fget() it or store
into an array. IMHO it's better to process one line at the time, as if the
uploaded file is a big one, then you will use more RAM if you store it in an
arrya.
> so I will need to somehow transfer the text of the .txt file into a
> variable. Is this what the explode() is used for? Once I get the array
> I think i'll be good. I was under the understanding that explode will
> create an array out of every new line. Would that look something like
> this:
> $myarray = explode("/n",$my_text_from_uploaded_file);
This would be the case if you had loaded the whole file into one variable,
remember that this will consume twice as much ram as the file is big, one in
$my_text_from_uploaded_file and one in $myarray
$fp = @fopen("/path/to/uploaded/file/inputfile.txt", "r");
/*We check that we can open the file*/
if ($fp) {
/* we can read the file, let loop one row/line at the time */
while (!feof($fp)) {
$text_line = fgets($fp, 4096);
/*Do something with the $text_line which contains one line of the text*/
mysql_query("INSER INTO table VALUE('$text_line')");
}
/* we are at the end of the file, we close it */
fclose($fp);
echo "File has been stored in the database!<br>\n";
} else {
/* Error message as we couldn't open the file */
echo "We couldn't open the file!!!<br>\n";
}
> When I run the query into the database I just want the text to be input
> in the database just like it is on the text document. Do I even need an
> array for this? Or is there a simpler function that will just copy the
> text to a variable that I can dump in the database?
No, you don't need an array, see the example above, it shows how you read one
line and inserts the date directly into the database. The examples this far
hasn't included much of error handling, I do suggest you do check at each step
that everything went okey, some problems you may be able to fix in the code,
others may not and you could make the script to mail you all errors, this way
you can manually insert those error lines.
> Let me know where you live and I'll talk you to dinner and drinks for
> helping me wiht this! :)
Looking at your posts header it seems that could be quite expensive for you,
I'm living on east shores of the Lake Atlantic.
//Aho
[Back to original message]
|