Writing to a text file

Discussion in 'Perl' started by Steven Mocking, Dec 14, 2003.

  1. 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
     
    Steven Mocking, Dec 14, 2003
    #1
    1. Advertising

  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
     
    Jürgen Exner, Dec 14, 2003
    #2
    1. Advertising

  3. Steven Mocking

    Nick Santos Guest

    "Neil Trigger" <> wrote in message
    news:KKWCb.54752$...
    > 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
     
    Nick Santos, Dec 14, 2003
    #3
  4. Steven Mocking

    Chris W Guest

    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
     
    Chris W, Dec 14, 2003
    #4
  5. Steven Mocking

    Alex Zeng Guest

    Use time stamp as a filename.

    "Neil Trigger" <> wrote in message
    news:KKWCb.54752$...
    > 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
    >
    >
     
    Alex Zeng, Dec 14, 2003
    #5
  6. Steven Mocking

    Neil Trigger Guest

    Neil Trigger, Dec 14, 2003
    #6
  7. Steven Mocking

    Nick Santos Guest

    very true. I'd forgotten about that one. thanks
     
    Nick Santos, Dec 14, 2003
    #7
  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
     
    Jürgen Exner, Dec 14, 2003
    #8
  9. Steven Mocking

    Nick Santos Guest

    "Jürgen Exner" <> wrote in message
    news:985Db.13855$...
    > [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
     
    Nick Santos, Dec 14, 2003
    #9
  10. Steven Mocking

    Neil Trigger Guest

    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
     
    Neil Trigger, Dec 15, 2003
    #10
  11. Steven Mocking

    Neil Trigger Guest

    Neil Trigger, Dec 15, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BLF
    Replies:
    4
    Views:
    1,282
    Mike Treseler
    Aug 7, 2004
  2. HNguyen
    Replies:
    4
    Views:
    2,408
    HNguyen
    Dec 21, 2004
  3. =?Utf-8?B?Y3dicDE1?=

    Writing a text file to the file system

    =?Utf-8?B?Y3dicDE1?=, Mar 21, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    523
    shiva_ananthoju
    Oct 29, 2005
  4. =?Utf-8?B?Y3dicA==?=

    Writing a text file to the file system

    =?Utf-8?B?Y3dicA==?=, Mar 21, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    322
    =?Utf-8?B?Y3dicA==?=
    Mar 21, 2005
  5. icedragon
    Replies:
    0
    Views:
    649
    icedragon
    Jul 13, 2010
Loading...

Share This Page