Start Codeschnipsel Perl Datei Upload Form

Datei Upload Form

0

Die Grundlage für ein Datei Upload Script

#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use File::Basename;

my $cgi = CGI->new;

print $cgi->header(-type => "text/html", -charset => "utf-8");

if ($cgi->request_method eq 'POST') {
    my $filename = $cgi->param('file');
    if ($filename) {
        my $upload_filehandle = $cgi->upload('file');
        my $target_dir = "./uploads";
        mkdir $target_dir unless -d $target_dir;
        my $basename = basename($filename);
        open (OUT, ">", "$target_dir/$basename") or die "Kann Datei nicht speichern: $!";
        binmode OUT;
        while (my $bytesread = <$upload_filehandle>) {
            print OUT $bytesread;
        }
        close OUT;
        print "<p>Datei erfolgreich hochgeladen: $basename</p>";
    } else {
        print "<p>Keine Datei ausgewählt!</p>";
    }
}

print <<'HTML';
<form method="POST" enctype="multipart/form-data">
  <input type="file" name="file">
  <input type="submit" value="Hochladen">
</form>
HTML

Keine Kommentare