1

I am struck at this point since 1 day. First am taking all file names .Then upload and collect all file handles. Due to some reason, files are not uploading into /home/img folder correctly. If uploaded, they are getting corrupted. Corrupted filesize shows as 0 bytes or 24 bytes.

Please provide assistance with upload part.

#!/usr/bin/perlml -Tw

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $cgi = CGI->new;

my $upload_dir = "/home/img";

my @raw_filenames       = $cgi->param('photos');
my $filename_characters = 'a-zA-Z0-9_.-';
my $ext                 = "";

my $raw_filenames = "";
my $raw_fh        = "";

my $single_filename  = "";
my $single_io_handle = "";

my @all_io_handle = "";
my @all_filenames = "";

print "Content-type: text/html\n\n";

#Getting the filenames ready
foreach $raw_filenames (@raw_filenames) {
    ( $single_filename, undef, $ext ) = fileparse( $raw_filenames, qr{\..*} );
    push( @all_filenames, "$single_filename" );
}

#This prints the ready list of filenames
print "@all_filenames\n <br>";

#Getting the filehandles ready
my @raw_fh = $cgi->upload('photos');

foreach $raw_fh (@raw_fh) {
    $single_io_handle = $raw_fh->handle;
    push( @all_io_handle, "$single_io_handle" );
}

#This prints the ready list of filehandles
print "@all_io_handle\n <br>";
11
  • 3
    Time to debug: Are you able to create a form to upload a single file successfully? Confirm that first. Then see if you can create a form that has multiple upload fields with unique names. Confirm that works. Finally, give all the upload fields the same name like you're trying here. This might isolate the problem to a smaller scope and therefore help with determining the issue.
    – Miller
    Sep 5, 2014 at 17:10
  • 2
    I know nothing about CGI uploads, but wondering why you didn't use some higher level and more modern framework, like: Poet, Mojolicious, Dancer and such... Many things are much simpler, easier and faster to develop-deploy...
    – clt60
    Sep 5, 2014 at 18:56
  • @Miller - Yes. i am able to create a form with code <form action="myperl.pl" method="post" enctype="multipart/form-data"> with input multiple attribute. Sep 6, 2014 at 11:07
  • The CGI module is looking rather long in the tooth these days. Check out CGI::Alternatives on CPAN to see more modern (and easier!) ways to do what you're trying to accomplish with cgi here. Sep 6, 2014 at 11:09
  • @Miller I will try using the method you said. I will get back again. Sep 6, 2014 at 11:15

1 Answer 1

0

The following test of CGI->upload works with html input multiple attribute.

I attempt to view the file size of each uploaded file both by the temp file and by reading it from disk:

#!/usr/bin/env perl
use strict;
use warnings;

use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $q = CGI->new();

my $msg = '';
if ( $q->request_method() eq 'POST' ) {
    $msg .= '<table border=1 cellpadding=3 cellspacing=0><tr><th>File name</th><th>Temp size</th><th>Slurped size</th></tr>';
    for my $file ( $q->upload('myfile') ) {
        my $tempsize = -s $q->tmpFileName($file);
        my $readsize = do {
            my $fh = $file->handle();
            binmode $fh;
            local $/;    # Slurp entire file
            length <$fh>;
        };
        $msg .= '<tr><td>' . $q->escapeHTML($file) . "</td><td>$tempsize</td><td>$readsize</td></tr>";
    }
    $msg .= '</table>';
}

print $q->header;

print <<"END_HTML";
<html>
<head>
<title>Upload form using CGI</title>
</head>
<body>
<h1>Upload form using CGI</h1>
$msg
@{[ $q->start_form() ]}
<p><input type="file" name="myfile" accept="image/gif,image/jpeg,image/png" multiple required /></p>
<p><input type="submit" value="Upload"></p>
@{[ $q->end_form() ]}
</body>
</html>
END_HTML

Before Posting:

Before Post

After Posting 3 images:

After Post

It's possible that you simple have an outdated version of CGI. Check the version

$ perl -MCGI -E 'say $CGI::VERSION'
4.04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.