Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

create new text file 2

Status
Not open for further replies.

brad1978

Programmer
Feb 13, 2009
30
CA
Hi all,
Quick question for you, how do I go about creating a new text file on the fly? I am trying to have a new text file created when a user submits a comment form. This file will contain the info submitted but I cannot seem to create a blank text file on the fly. Any help would be appreciated.
Brad
 
you mean something like this?

if (param())
{
my $id = param('id');

#define filehandle name
my $fname = $id . ".txt";

#print filehandle
print <$fname>;
}
 
Thanks for the help. This still doesn't seem to create the text file... What am I doing wrong?

#define and create textfile for replies
my $checkid = $id.".txt";

open(FH,">$checkid") or die "$!";
close(FH);
 
it should print a message letting you know why, that what or die "$!" is there for. If its a CGI script add this line to your code:

use CGI::Carp qw/:fatlasToBrowser/;

it will print the necessary headers in case of an early termination of the script. If you are already printing a complete http header you don't need to add it.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The $checkid is what I was hoping for. The $id is a random generated number from javascript that I am setting as the value of ID through a substring call. By calling $checkid = $id . ".txt"; I get the value I want to name my text file as it just won't create the file. Was that last post to be fataltobrowsers?
 
If your script is in the CGI-BIN that could be a problem.
Try a simple file creation to check
Code:
open(FH,">".'file.txt') or die "$!";
print FH "test\n";
close FH;
then try back a level.
Code:
open(FH,">".'../file.txt') or die "$!";
print FH "test\n";
close FH;


Keith
 
Thanks!
I'll try that tomorrow. I really appreciate the help!
Brad
 
I made a mistake in my previous post, should be:

use CGI::Carp qw/:fatalsToBroswer/;

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hey audiopro,
thanks for the tip. It worked like a charm. If I replace the variable in the file handle with a generic name like test.txt, the file gets created. But if I replace the test.txt with the variable it doesn't work.

WORKS
open(FH,">>test.txt") or die "Cannot create file";
print FH "";
close(FH);


DOESN'T WORK
#define and create textfile for replies
my $checkid = $id.".txt";
print $checkid;

open(FH,">>$checkid") or die "Cannot create file";
print FH "";
close(FH);

Any thoughts?!
 
when I print out $checkid to see the value is does print as 1234.txt or what ever $id is passed in. Do I need to print it in the file handle as open(FH,">>.scalar($checkid))or die "Cannot create file"; or something? I wish I could use the /fatalstobrower/ to see the error code, but I don't administer the server and don't have that module installed. Here is my full code (if it helps).

Code:
#!/usr/bin/perl -wT
print header(-expires=>'-1d');
use CGI qw/:standard/;

if (param()) 
	{
    my $id = param('id');
		my $name = param('name');
		my $email = param('email');
		my $title = param('title');
		my $details = param('details');
				
    open(OUT, ">>topics.txt") or die "Cannot write to file.";
      print OUT "$id \t $name \t $email \t  $title \t $details\n";		
    close(OUT);
    
    #define and create textfile for replies
    my $checkid = $id.".txt";

    
    open(FH,">>$checkid") or die "Cannot create file";
      print FH "";
    close(FH);

    
    print "<br /><br />Your comments have been submitted successfully!  Please <a href='index.htm' class='text'>click here</a> to view all topics";
	}
else
	{
		print "Error, please <a href='newTopic.htm'>try again</a>.";
	}
 
I assume the topics file is being written to ok.

leave the code as it is but add the extra lines.
ok try
Code:
    #define and create textfile for replies
[red]chomp $id[/red]    
    my $checkid = $id.".txt";
does that create a file?
Code:
    #define and create textfile for replies
[red]$id='test'[/red]
    my $checkid = $id.".txt";
does that create a file?
Code:
    #define and create textfile for replies
[red]my $LENF=length($id);
print "$LENF";[/red]
    my $checkid = $id.".txt";
Is that the correct number of chars ie. 4 for 1234.
You may have an odd char clinging on to the string for dear life


Keith
 
HA HA HA, thanks audiopro! I needed that! You may be right. I totally forgot to chomp the $id variable. I'll test it and let you know. Thanks for the help, I'm fairly new to perl so this is helping me learn!
Cheers
 
WOW, now it's getting narrowed down. audiopro, your second test worked and created the file!

#define and create textfile for replies
$id='test';
my $checkid = $id.".txt";

So something is wrong with my $id variable. I am chomping it, the length is correct, I don't get it... Is it possible that the text file cannot begin with a number?
 
The problem may be that you are using the -T switch on the shebang line and trying to use a value that comes from outside the script without untainting it first. You should be getting an error about an "insecure dependency". If not, then this line:

Code:
 my $checkid = $id.".txt";

is satisfying the -T switch and allowing your script to use the passed in data to create a file. But, what you are doing is very unwise unless you are just doing some preliminary testing and plan on changing that later.

Read this article:



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top