Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks.

I have a file uploader on my site that works when you upload from the browser. It accepts any image type. I want to have a CMD version so I can upload an entire directory of them at a time since the server seems to kill me after about 2 minutes of processing if uploading a huge zip file.

Anyway, the problem.. I've tried about 7 images and this script fails every time. The content print out at the end generally shows the HTML of the first page (pre-submitting) so it never shows my status messages or warnings of any kind. Once in a dozen tries it does get the nice ISE 500 though. But essentially I don't know what the problem could be, the html dump on error doesn't show that the form was ever submitted.

The form fields are correct and there's only one form on the page.

#!/usr/bin/perl use warnings; use strict; use FindBin '$Bin'; my $currentdir = "$Bin"; my $url = ""; opendir (DIR, "$currentdir") or die "Error: $!"; my @images = grep { /\.(?:png|gif|jpg)$/i } readdir DIR; closedir DIR; print "\nCategory Name: "; my $category = <STDIN>; chomp($category); print "\nName: "; my $name = <STDIN>; chomp($name); print "\nDescription: "; my $description = <STDIN>; chomp($description); my $cnt = 0; my $cnt2 = 0; foreach my $image (@images) { $cnt++; } print "\nWe found $cnt images"; foreach my $image (@images) { $cnt2++; print "\n\t Trying picture $currentdir/$image - $cnt2 of $cnt\n"; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); $mech->get( $url ); $mech->submit_form( form_number => 1, fields => { file => "$currentdir/$image", category => "$category", name => "$name", description => "$description" } ); my $source = $mech->content; if ($source =~ m/success/i) { print "$image succeeded\n"; } else { print "$image failed\n"; print $mech->content; exit; } }

Replies are listed 'Best First'.
Re: Creating a short image uploader
by svenXY (Deacon) on Jun 09, 2008 at 07:17 UTC
    Hi,
    it's a bit hard to help you because without the HTML-Code of the Upload-Site (especially the form), we can't test this here. It doesn't look too bad at first glance, though. Are you getting any error messages on the console?

    Some thoughts:
    print "\nWe found $cnt images"; # don't "use" the module in the foreach loop otherwise it's done each +time use WWW::Mechanize if $cnt > 0; unless ($cnt > 0) { print "No images selected\n"; exit 1; } my $mech = WWW::Mechanize->new(); foreach my $image (@images) $cnt2++; print "\n\t Trying picture $currentdir/$image - $cnt2 of $cnt\n"; $mech->get( $url ); # do you have the correct form here? print $mech->content; $mech->submit_form( form_number => 1, # sure? this could be one problem fields => { file => "$currentdir/$image", category => "$category", name => "$name", description => "$description" } );

    If that does not help, I can only - as always - suggest to use Corion's WWW::Mechanize::Shell - it will allow you to interactively fill out the form (on the commandline) and after having done so successfully, automagically print out your steps as valid perl code - for me this is much easier than to write the code in the first place...
    Good luck,
    svenXY