Perl script to display files in a folder (and delete them) via HTML

Posted by sgj700 on Jun 16, 2008 in bash and perl |

Perl script to display files in a folder (and delete them) via HTML

Place this code into an “index.cgi” file, and open that file in a browser:

#!/usr/bin/perl
$dir = “<absolute path to the directory you’d like to see contents of>”;
$folder = “http://<your web server path to that directory>”;

use CGI; # load the CGI.pm module
my $GET = new CGI; # create a new object
my @VAL = $GET->param; #get all form field names

my $filename = $GET->param($_[0]);
&PrintHead; #print the header

if($GET->param(‘delfile’)) {
print “<hr><form>Are you sure you want to delete the <b>”.$GET->param(‘delfile’).”</b> file?
<input type=submit value=Delete name=”.$GET->param(‘delfile’).”>
<input type=submit name=cancel value=Cancel>
<input type=hidden name=del_confirm value=”.$GET->param(‘delfile’).”>

</form>
<hr>
<br>”;
}
if($GET->param(‘del_confirm’) and !$GET->param(‘cancel’)) {
if (unlink($GET->param(‘del_confirm’)) == 0) {
}
print “<hr><form>The <b>”.$GET->param(‘del_confirm’).”</b> file has been deleted.”;
}

print qq~
<html>
~;

print qq~
<table align=”center” width=”500″>
<tr><td><font face=”Arial” size=”2″><b>Current files in folder</b></td></tr>
<tr>
<td valign=”top”>
<font face=”Arial” size=”2″>
~;

opendir(DIR, “$dir”);
@current = readdir(DIR);
closedir(DIR);

foreach(@current){
unless($_ eq ‘.’ || $_ eq ‘..’ || $_ eq ‘.htaccess’ || $_ =~ ‘index.cgi’ || -d qq~$dir/$_~){
push(@currentfiles, $_);
}
}
@currentfiles = sort { uc($a) cmp uc($b) } @currentfiles;

for($aa = 0; $aa <= int($#currentfiles / 2); $aa++){
print qq~
<font color=”#ff0000″><b>&#149;</b>
<a href=”$folder/$currentfiles[$aa]” target=”_blank”>$currentfiles[$aa]</a><a href=”index.cgi?delfile=$currentfiles[$aa]“> (Delete)</a></font><br>
~;
}

print qq~</font></td><td valign=”top”><font face=”Arial” size=”2″>~;

for($bb = $aa; $bb < @currentfiles; $bb++){
print qq~
<font color=”#ff0000″><b>&#149;</b>
<a href=”$folder/$currentfiles[$bb]” target=”_blank”>$currentfiles[$bb]</a><a href=”index.cgi?delfile=$currentfiles[$bb]“> (Delete)</a></font><br>
~;
}

print qq~
</font></td>
</tr>
</table>
</html>~;
&PrintFoot; #print the footer

sub PrintHead {

print qq~Content-type: text/html\n\n~;
print qq~
<html>
<title>Peds Uploads</title>
<body bgcolor=”#ffffff”>
~;
}

####################################################################
#
#####################################################################
#
sub PrintFoot {
print qq~
</body>
</html>
~;

1 Comment

Perl Scripting
Jun 24, 2008 at 9:48 am

found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later ..


 

Copyright © 2010 Sandon Jurowski All rights reserved.
Desk Mess Mirrored v1.2 theme from BuyNowShop.com.