Writing to a text file

S

Steven Mocking

Neil said:
Is there a way of creating a seperate text file on a server every time a
form is sent?

Certainly, but why don't you use a database? A clogged up directory full
of thousands of text-files bites you in the end.
 
N

Nick Santos

Neil Trigger said:
Is there a way of creating a seperate text file on a server every time a
form is sent?

I agree. I recently wrote a website that requires lots of coding and lots of
forms. Each form did a lot of text file reading and writing. I only have
about 30 users (who can use forms multiple times), but I have about 650
files and 450 folders for this. It's not the way to go. I've been slowly
switching it over to MySQL.

But to answer your question, yes. Therea are two ways you could do it
(assuming they aren't logged in. If they are logged in then you can do many
more). But If they aren't logged in, you can create a text file by ip
address, or text files can be created sequentially. You can also creat it
using any other data that you have on the user. If you create it by ip
address, you will want to append to the file if the user is allowed to
submit the form multiple times.

#_BEGIN_
# this is a demonstration of using the ip address to do it
# the ip address can be gotten using the following variable
$ENV{'REMOTE_ADDR'}

open (OUTF, ">>$ENV{'REMOTE_ADDR'}.txt"); #appending to prevent data loss
print OUTF "data";
close (OUTF);

#_END_

That is definitely the simpler one, assuming that the people aren't
submitting it multiple times.

#_BEGIN_
# This is a demonstration of making a text file by sequential numbering

opendir (DIR, "filedir");
@dir = readdir (DIR); #get all the files in the directory's names
closedir (DIR);

$end = @dir;
$i = 2; #we start i at two because of the . and .. entries in the directory

while($i<$end){
if(@dir[$i] =~ /\d.txt/){ #count the number of files in the directory
that are already made by this program
$n++; #n will eventually be the digit we use in the filename
}
$i++;
}

open (OUTF, ">>$n.txt"); #still good to append just to prevent data loss
print OUTF "data";
close (OUTF);

#_END_

that one will create a seperate file and number them sequentially. If you
remove files from the directory though, be careful because it could start
writing in other ones and you won't know. If you want to be able to remove
files and have it still continure after the last one, then we'd check the
whole array using regular expressions for what the highest one is. I won't
put code for that here because I'm not sure if you'll be able to use it, and
I'm fairly bad at regular expressions. However, if you do decide you could
use it, I'd be happy to post some code.
-Nick
 
C

Chris W

Steven said:
Certainly, but why don't you use a database? A clogged up directory full
of thousands of text-files bites you in the end.

I agree, but if you have an application that doesn't see very heavy use
and don't have a db on the server you are using like this one project I
have done. What you can do is have your script get the time then append
000 to the end, then try to open for reading a file of that name, if it
opens you know it already exist so then move on to 001 and see if that
exists, keep going till find a file that doesn't exist and then create
it. This isn't the best solution and will cause problems if it has
heavy use but for simple projects that are low usage it works fine.
Another option would be to append the IP address of the incoming request
and the port number the client is using although I'm not sure how to
find the port the client is sending on but there must be a way.

Chris W
 
N

Neil Trigger

Is there a way of creating a seperate text file on a server every time a
form is sent?
 
N

Nick Santos

Jürgen Exner said:
[fullquote]
Nick said:
very true.

What is true?
I'd forgotten about that one.

What did you forget about?

Thanks for what?

Without some sort of context your statement is incomprehensible.

jue

I'd forgotten about using a timestamp as a filename. one sure way to not get
overwrites as long as it's specified well enough. I don't usually delete the
content, but for some reason I did with that one. Sorry about that
 
N

Neil Trigger

Thank you all for your advise. I thought that the text file'd be the best
way, but upon reflection I think a database might be the best way. I have a
MySQL database system thing on my webserver that I've not played with to any
great (or very clever) extent.
Would I need to subscribe to alt.database.integration or something and ask
them? ;o)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top