View Full Version : Deletion filter
No--Name
07-23-2005, 08:24 PM
I actually don't understand anything of perl, and just googled this script :p So please don't talk expert to me, just tell me what to do, thanks.
This is my nice lil' piece of code, to delete a specific file:
#!/usr/bin/perl -w
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print "Content-type: text/html\n\n";
@files = glob "*.txt";
foreach $file (@files) {
print "<a href='file'>$file</a><br>\n";
}
($fntd) = ($ENV{QUERY_STRING} =~ /file=(.*)/);
unlink "$ENV{DOCUMENT_ROOT}./uploads/$fntd" or die "Unable to remove file: $!";
with $fntd as filename given by another script. Now is my question: how can I simply edit this script, so it's only able to delete files with a specific extension?
FishMonger
07-23-2005, 08:46 PM
I'm temped but since you asked us not to, I won't say anything about what's wrong with that code.
On to answering your question. There are several ways you can limit the deletion to 1 or more file extensions; here's one.
($fntd) = ($ENV{QUERY_STRING} =~ /file=(.*)/);
if ($fntd =~ /\.txt$/) {
unlink "$ENV{DOCUMENT_ROOT}./uploads/$fntd" or die "Unable to remove file: $!";
}
No--Name
07-24-2005, 12:47 PM
It worked! Thanks, FishMonger! ;)
Btw:
I'm temped but since you asked us not to, I won't say anything about what's wrong with that code.
Well, because you're so tempted, please do it anyway. My programming skills are not 'zero', and I like to improve my script ofcourse. Is it insecure, or anything? Remember when replying, like I said in my previous post, that my Perl skills are minimal, so keep it easy, thanks.
FishMonger
07-24-2005, 11:09 PM
Actually, I was just playing with you. Most of the time when someone says "don't talk expert to me, just tell me what to do", they know that thier code is poorly written but they don't care and only want to get thier borken code running any way the can. Your code, while not being the great, isn't too bad. However, there are some things that I'll bring to your attention; some of which are personal preferences in coding style.
1) Most experienced Perl programmers say you should always without exception use the strict pragma.
http://www.unix.org.ua/orelly/perl/prog3/ch31_19.htm
http://www.developer.com/lang/perl/article.phpr/3323421
2) It's good to see that you are using the CGI::Carp module however, there's a gotcha that you need to know. The use statement imports the fatalsToBowser and warningsToBrowser subroutines but unlike fatalsToBowser, warningsToBrowser still needs to be enabled. You need to call the subroutine as early as possible but after printing the header and pass 1 as it's paramature.
warningsToBrowser(1);
3) I've seen experienced Perl programmers, from time to time, goof up printing header statement: print "Content-type: text/html\n\n";
I think it's better to use header (method/function) from the CGI module.
print header(); # function method
or
print $q->header; # object method
4) The glob and foreach loop can be combined.
foreach my $file (<*.txt>) {
5) Use the CGI module for retrieving the QUERY_STRING. This will alow you to retrieve either POST or GET submissions with the exact same code.
6) Test for file existance before the unlink.
7) Check you syntax on the link, I think you have a typo.
Did you mean
href='file'
to be
href='$file'
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print header();
warningsToBrowser(1);
foreach my $file (<*.txt>) {
print "<a href='$file'>$file</a><br>\n";
}
my $fntd = param('file');
if ($fntd && -e $fntd) {
unlink "$ENV{DOCUMENT_ROOT}./uploads/$fntd" or die "Unable to remove file: $!";
}
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.