469,949 Members | 2,212 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,949 developers. It's quick & easy.

Writing to a text file

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

--
¿ Trigger ?
http://www.magic2k.com/
http://www.oddmap.co.uk
Jul 19 '05 #1
10 4410
Neil Trigger wrote:
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.

--
Nobody ever forgets where he buried the hatchet.
-- Kin Hubbard
Jul 19 '05 #2
Neil Trigger wrote:
Is there a way of creating a seperate text file on a server every
time a form is sent?


Where is the problem?

jue
Jul 19 '05 #3

"Neil Trigger" <em***@magic2k.com> wrote in message
news:KK*******************@news-binary.blueyonder.co.uk...
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
Jul 19 '05 #4
Steven Mocking wrote:

Neil Trigger wrote:
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.


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
Jul 19 '05 #5
Use time stamp as a filename.

"Neil Trigger" <em***@magic2k.com> wrote in message
news:KK*******************@news-binary.blueyonder.co.uk...
Is there a way of creating a seperate text file on a server every time a
form is sent?

--
¿ Trigger ?
http://www.magic2k.com/
http://www.oddmap.co.uk

Jul 19 '05 #6
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)

--
¿ Trigger ?
http://www.magic2k.com/
http://www.oddmap.co.uk
Jul 19 '05 #7
very true. I'd forgotten about that one. thanks
Jul 19 '05 #8
[fullquote]
Nick Santos wrote:
very true.
What is true?
I'd forgotten about that one.
What did you forget about?
thanks


Thanks for what?

Without some sort of context your statement is incomprehensible.

jue
Jul 19 '05 #9
"Jürgen Exner" <ju******@hotmail.com> wrote in message
news:98****************@nwrddc01.gnilink.net...
[fullquote]
Nick Santos wrote:
very true.


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


What did you forget about?
thanks


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
Jul 19 '05 #10
Thanks everyone for your help.
You've posted a lot of useful info.

--
¿ Trigger ?
http://www.magic2k.com/
http://www.oddmap.co.uk
Jul 19 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by melanieab | last post: by
3 posts views Thread by CsharpNewcommer | last post: by
reply views Thread by Yunus's Group | last post: by
3 posts views Thread by nicolasg | last post: by
5 posts views Thread by grinder | last post: by
6 posts views Thread by arne.muller | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.