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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create file and folder with CGI

Status
Not open for further replies.

NateBro

Programmer
Sep 1, 2004
233
US
This is what i'm going for, but it douse not work.

I whant to create a file and folder with ver. but i'm not sure how to. any help would be cool.

This is what i have got so far.



#!/usr/bin/perl
use CGI qw:)standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

print header;
print start_html("Results");

my $folder = param('folder');
my $file = param('file');

open(fileOUT, ">.../$folder/$file.html") or dienice("Can't open .../$folder/$file.html for writing: $!");
flock(fileOUT, 2);
seek(fileOUT, 0, 2);
print fileOUT "<center>~Blank~</center>\n";
print fileOUT "\n\n";
close(fileOUT);

print <<EndHTML;

<script>
window.alert("This file is blank!")
</script>

EndHTML

print end_html;

sub dienice {
my($errmsg) = @_;
print "<h2>Error</h2>\n";
print "<p>$errmsg</p>\n";
print end_html;
exit;
}
 
Try this:

Code:
#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

print header;
print start_html("Results");

my $folder = param('folder');
my $file = param('file');

[COLOR=red]
if(!(-e "$folder"))
{
    mkdir("$folder", 0777) or die "Cannot create output directory $folder: $!";
}
[/color]

open(fileOUT, ">.../$folder/$file.html") or dienice("Can't open .../$folder/$file.html for writing: $!");
flock(fileOUT, 2);
seek(fileOUT, 0, 2);
print fileOUT "<center>~Blank~</center>\n";
print fileOUT "\n\n";
close(fileOUT);

print <<EndHTML;

<script>
window.alert("This file is blank!")
</script>

EndHTML

print end_html;

sub dienice {
    my($errmsg) = @_;
    print "<h2>Error</h2>\n";
    print "<p>$errmsg</p>\n";
    print end_html;
    exit;
}
 
it didnt work, i dont know why...


Error
Can't open .../newtest/newtest.html for writing: No such file or directory
 
OK, so I would have a bug in my addition (That'll teach me to rush).

First, the change to my code.
Code:
if(!(-e "$folder"))
{
    mkdir("/path/from/root/$folder", 0777) or die "Cannot create output directory $folder: $!";
}

Your script should have died if it couldn't create the directory, so if you look, the new directory exists as a sub-directory of where your script is running from. So, if you're running this in cgi-bin, you'll now have cgi-bin/newtest as well.

- Rieekan
 
still err, i don't know, i haven't been doing cgi very long.


Content-type: text/html

Software error:

Cannot create output directory dousethiswork: No such file or directory at /somethig/somethig/somethig/somethig/somethig/somethig/cgi_bin/newfolder.cgi line 15.

 
This does sound like a permission problem for the script. The script is dying at the creation of the directory.
 
I saw something else in your program that might be causing your issue. You have the following as your file open statement:
Code:
open(fileOUT, ">.../$folder/$file.html") or dienice("Can't open .../$folder/$file.html for writing: $!");
The three dots (...) seem to be causing an issue when trying to open the file. Change that to match where you're creating the directory
Code:
open(fileOUT, ">/path/from/root/$folder/$file.html") or dienice("Can't open path/from/root/$folder/$file.html for writing: $!");

Otherwise, I don't see any issues and can get the script to work on the server I have here at work.

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top