Creating a new text file 'on the fly'?

I have a program that allows a new user to register.  I have a text file holding certain information for each user.  Once a new user registers is there a way that I can create a new text file right then possibly naming it $username.txt?  I can find information about opening, reading, and writing to files online but I'm unsure if it's possible to create one 'on the fly' so to say.  Thanks (I'm a beginner with perl/cgi scripts).
briehnAsked:
Who is Participating?
 
ItatsumakiConnect With a Mentor Commented:
There are dozens of security implications with this, so be careful, but...

#############
my $outfile = $username.'.txt';
open(OUT, ">$outfile");
print OUT "Tester\n";
close(OUT);
#############

Caution:
If you already have a file of this name it will be over written!  If you want to append, use:

#############
my $outfile = $username.'.txt';
open(OUT, ">>$outfile");
print OUT "Tester\n";
close(OUT);
#############

If you want to lock the file for exclusive access first, try this:

#############
my $outfile = $username.'.txt';
open(OUT, ">$outfile");

if (!flock(OUT, 2)) {
    die "Couldn't lock file\n";
    }

print OUT "Tester\n";
close(OUT);
#############

Finally, if you need to check if the file exists, just do:

#############
my $outfile = $username.'.txt';

if (open(IN, "<$outfile")) {
    die "File already exists!\n";
    }

open(OUT, ">$outfile");

if (!flock(OUT, 2)) {
    die "Couldn't lock file\n";
    }

print OUT "Tester\n";
close(OUT);
#############
0
 
BassTeQCommented:
Of course you can create a text file on the fly, how are you going to trigger this script though ? Via a link, form etc ...?
0
 
briehnAuthor Commented:
Thanks, this was really helpful!
0
All Courses

From novice to tech pro — start learning today.