?
Solved

Creating a new text file 'on the fly'?

Posted on 2003-03-19
3
Medium Priority
?
135 Views
Last Modified: 2013-12-25
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).
0
Comment
Question by:briehn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Expert Comment

by:BassTeQ
ID: 8171625
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
 
LVL 2

Accepted Solution

by:
Itatsumaki earned 300 total points
ID: 8177921
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
 

Author Comment

by:briehn
ID: 8178160
Thanks, this was really helpful!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question