Problem with CGI (500 Internal Server Error) Need Help
Hello,
I want to empty selected folder and then remove it.
My code is below:
delete.cgi
Code:
#!/usr/bin/perl
use CGI;
use CGI qw/:standard/;
use Config;
my $cg = new CGI;
my $ID_IDENTIFIER = $cg->param('ID_IDENTIFIER');
#my $FILE_IDENTIFIER = $cg->param('FILE_IDENTIFIER');
my $target_dir = "$c->{target_dir}/$ID_IDENTIFIER";
#my $target_file = "$c->{target_dir}/$ID_IDENTIFIER/$FILE_IDENTIFIER";
&DelData($target_dir);
sub DelData
{
my ($dir) = @_;
opendir(DIR, $dir) || die"Hata";
my @ff = readdir(DIR);
closedir(DIR);
for my $fn(@ff)
{
unlink("$dir/$fn");
print("$dir/$fn has been empty");
}
rmdir("$dir");
print("$dir has been deleted");
}
will redirect most fatal errors to the browser. It's helpfule for debugging scripts.
It looks to me like the first problem and maybe the only problem, is that you have not printed a header before trying to print some data to the browser, change this:
Code:
my $cg = new CGI;
my $ID_IDENTIFIER = $cg->param('ID_IDENTIFIER');
to:
Code:
my $cg = new CGI;
print $cg->header;
my $ID_IDENTIFIER = $cg->param('ID_IDENTIFIER');
#!/usr/bin/perl
use CGI;
use Config;
use File::Path;
my $cg = new CGI;
print $cg->header,$cg->start_html;
my $ID_IDENTIFIER = $cg->param('ID_IDENTIFIER');
#my $FILE_IDENTIFIER = $cg->param('FILE_IDENTIFIER');
my $target_dir = "$c->{target_dir}/$ID_IDENTIFIER";
#my $target_file = "$c->{target_dir}/$ID_IDENTIFIER/$FILE_IDENTIFIER";
rmtree($target_dir, 1, 1);
print $cg->end_html;
also, passing a filename as a CGI parameter to the script and not doing any validation/taint checking is insecure. If you are the only person with access to the script then I assume you know what you are doing. If this script will be used by the public you will want to do some validation and taint checking on any variables used for possible insecure operations, such as deleting files/directories. Read up here if interested:
I have try to use rmtree but it doesn't work. I added print $cg->header; it works perfect. And also use CGI::Carp qw/fatalsToBrowser/; showed the error on opendir(DIR, $dir)
So I have changed my codes like this:
Code:
#!/usr/bin/perl
use CGI;
use Config;
use File::Path;
my $cg = new CGI;
print $cg->header,$cg->start_html;
my $ID_IDENTIFIER = $cg->param('ID_IDENTIFIER');
my $FILE_IDENTIFIER = $cg->param('FILE_IDENTIFIER');
my $target_dir = "$c->{target_dir}/$ID_IDENTIFIER";
my $target_file = "$c->{target_dir}/$ID_IDENTIFIER/$FILE_IDENTIFIER";
unlink("$target_file");
rmdir("$target_dir");
print("$target_file has been deleted<br>$target_dir removed");
print $cg->end_html;