Created
March 21, 2011 14:19
-
-
Save yukioc/879507 to your computer and use it in GitHub Desktop.
mini CGI file uploader and browser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| # mini uploader ver.0.1 (2011-03-21) by yukioc | |
| # Ver.0.1 2011-03-21 new. | |
| use strict; | |
| use warnings; | |
| use POSIX 'strftime'; | |
| use CGI; | |
| use CGI::Carp 'fatalsToBrowser'; | |
| use File::Basename 'basename'; | |
| use HTML::Template; | |
| use utf8; | |
| my $q=CGI->new; | |
| my $app="mini uploader ver.0.1 (2011-03-21)"; | |
| my $write_dir='.'; | |
| my $title="mini uploader"; | |
| my $log; | |
| my $cmd=$q->param('q'); | |
| if($cmd){ | |
| my $name=$q->param('name'); | |
| my $path="$write_dir/".$name; | |
| if($cmd eq 'up'){ | |
| if(-e $path){ | |
| $log="ERROR: file already exists : $name"; | |
| }else{ | |
| if(my $fh=$q->upload('name')){ | |
| open my $fo,">",$path or die "$path:$!"; | |
| print $fo $_ while(<$fh>); | |
| close $fo; | |
| $log="upload done : $name"; | |
| }else{ | |
| $log="ERROR: upload failure : $name"; | |
| } | |
| } | |
| }elsif($cmd eq 'del'){ | |
| if(-e $path){ | |
| if(-d $path){ | |
| $log="ERROR: can't delete directory: $name"; | |
| }elsif(!-o $path || !-W $path){ | |
| $log="ERROR: can't permission: $name"; | |
| }elsif(unlink $path){ | |
| $log="delete done: $name"; | |
| }else{ | |
| $log="ERROR: delete failure: $name"; | |
| } | |
| }else{ | |
| $log="ERROR: file not found : $name"; | |
| } | |
| } | |
| } | |
| my @flist= sort {$b->{mtime} cmp $a->{mtime}} map {{ | |
| url=>$_, | |
| name=>basename($_), | |
| size=> -s $_, | |
| mtime=>strftime "%F %T",localtime((stat $_)[9]), | |
| }} glob "$write_dir/*"; | |
| my $template=join('',<DATA>); | |
| my $t=HTML::Template->new(scalarref=>\$template); | |
| $t->param(log=>$log) if($log); | |
| $t->param(app=>$app); | |
| $t->param(title=>$title); | |
| $t->param(flist=>\@flist); | |
| print $q->header(-type=>'text/html',-charset=>'utf-8'), $t->output; | |
| __DATA__ | |
| <html lang="ja"> | |
| <head> | |
| <title><TMPL_VAR name=title></title> | |
| <style type="text/css"><!-- | |
| * {margin:0;padding:0;} | |
| body {font-size:small;margin:5px;} | |
| .ftable th {background-color:#CCC;} | |
| #name {background-color:#CCC;padding:2px;} | |
| .up {border:1px ridge gray;margin:5px 0;} | |
| .log {font-style:italic;color:red;margin:5px 0;} | |
| --> | |
| </style> | |
| </head> | |
| <body> | |
| <h1><TMPL_VAR name=title></h1> | |
| <!--log--> | |
| <TMPL IF name=log> | |
| <div class="log"><TMPL_VAR name=log></div> | |
| </TMPL IF> | |
| <!--upload--> | |
| <form method="post" enctype="multipart/form-data"> | |
| <input type="hidden" name="q" value="up"/> | |
| <table class="up"><tbody><tr> | |
| <td><input type="file" name="name" id="name"/></td> | |
| <td align="right"><input type="submit" name="submit" value="upload"/></td> | |
| </tr></tbody></table> | |
| </form> | |
| <!--files--> | |
| <table class="ftable"><tbody> | |
| <tr><th>name</th><th>last modified</th><th>size</th></tr> | |
| <TMPL_LOOP NAME=flist> | |
| <tr> | |
| <td><a href="<TMPL_VAR name=url>"><TMPL_VAR name=name></a></td> | |
| <td><TMPL_VAR name=mtime></th> | |
| <td align=right><TMPL_VAR name=size></td> | |
| <td><form method="post"> | |
| <input type="hidden" name="q" value="del" /> | |
| <input type="hidden" name="name" value="<TMPL_VAR name=name>" /> | |
| <input type="button" value="del" onclick='if(confirm("Do you want to delete this file ?\n\n<TMPL_VAR name=name>")==true)submit()'/> | |
| </form></td> | |
| </tr> | |
| </TMPL_LOOP> | |
| </tbody></table> | |
| <hr/> | |
| <small><i><TMPL_VAR name=app></i></small> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment