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

Is it possible to upload a file from a web form without using

$fh = $query->upload('uploaded_file');

the CGI.pm that I am using is old and does not have it.

I have tried

$filename = $query->param('uploaded_file'); open (OUTFILE,">>/usr/local/web/users/feedback"); while ($bytesread=read($filename,$buffer,1024)) { print OUTFILE $buffer; }

However I keep getting a file size of 0

Desparate!

Replies are listed 'Best First'.
Re: Upload a file from a web form
by choocroot (Friar) on Jul 09, 2003 at 10:26 UTC
    $filename is not a filehandle. The "param()" method returns the file name but not an opened filehandle to it. This file name cannot be opened directly with an "open()" as the file does not exist on your filesystem, it is the file name on the client host.

    If your CGI.pm does not handle the "upload()" method you can upgrade it (with perl -MCPAN -e 'install CGI') or, if you don't have root access to the webserver, grab an updated CGI.pm module file and put it in a directory on your webserver and load it in your scripts with:
    use lib "/where/is/your/cgi/module"; use CGI;
    That should load this newest module instead of the system default module.

    update: cfreak is right and i'm absolutely wrong. I checked the doc, and param() on an upload field returns a filename that is also a filehandle. Thanks cfreak.

      Not, true. If your param is an uploaded file field it works as both the filename or the filehandle depending on the context you use it in. The upload() method was added later to be less confusing.

      Lobster Aliens Are attacking the world!
      Tried that and get the following error:

      Parameter to use lib must be directory, not file

        Of course, in my "code", you should understand the "/where/is/your/cgi/module" string as the directory name in which you have CGI.pm and not the full path name to the CGI.pm file (/where/is/your/cgi/module/CGI.pm) ...
Re: Upload a file from a web form
by ColtsFoot (Chaplain) on Jul 09, 2003 at 10:09 UTC
    Without a little bit more of your code its difficult to say
    are you specifying the correct enctype in the <form> tag?
    <form method=post action='script.pl'enctype='multipart/form-data'>
    or if you're using mod CGI (as you should be)

    $page->start_mulitpart_form();

    I think not doing this caused your situation to happen to me once

    HTH
      Yes, specified the enctype... still no success!
Re: Upload a file from a web form
by antirice (Priest) on Jul 09, 2003 at 09:40 UTC

    Are you certain you have the proper permissions on /usr/local/web/users/feedback? Try this:

    open (OUTFILE,">>/usr/local/web/users/feedback") or die "Error trying +to open outfile: $!";

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      Yep, tried that, don't get any errors. The file is created, however its size is zero.
Re: Upload a file from a web form
by barrd (Canon) on Jul 09, 2003 at 10:10 UTC
    Hi bodmin,

    Your code doesn't make much sense, you appear to be attempting to open a directory rather than a filehandle:

    open (OUTFILE,">>/usr/local/web/users/feedback");

    How about something like:

    #!/usr/bin/perl -w use strict; use CGI qw/:standard/; my $query = CGI->new(); my $file = "/usr/local/web/users/feedback/foo.bar"; open (WRITE, ">$file") || die "Couldn't open $file: $!\n"; my $fh = $query->param('upload_file'); print WRITE while (<$fh>); close (WRITE) || die "Couldn't upload $file: $!\n";

    HTH ~ barrd

    Update: antirice is correct, I posted this before coffee... "/me must wake up before (speaking||writing)"... I also /msg'd (him||her) to acknowledge that fact.

      Just a note: a filename doesn't require an extension and a directory's name can contain periods.

      antirice    
      The first rule of Perl club is - use Perl
      The
      ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Upload a file from a web form
by dws (Chancellor) on Jul 09, 2003 at 15:52 UTC
    the CGI.pm that I am using is old and does not have it.

    What prevents you from either (a) getting CGI.pm updated, or (b) using a private copy?

      Well, managed to get CGI.pm updated, however I still generate files with zero size!

      If I have permission to create a file does it mean that I also have permission to write to that particular file?

        Well, managed to get CGI.pm updated, however I still generate files with zero size! If I have permission to create a file does it mean that I also have permission to write to that particular file?

        If you can create it, you can write to it (at least on Win32 and Unix (or Unix-like) platforms).

        I'm puzzled on this one. I switch my upload script from using the

        while ( <$filename> ) { ...
        approach, and tried the
        while ( read($filename, $_, 1024) ) { ...
        form, and both worked (as I expected they would).

        Having the read() return 0 is the only thing I could see (based on what you posted) that would cause an empty file. Since CGI.pm reads and caches the incoming file first, there might be an issue with something like the filename. Can you post the HTML form that you're using to trigger the upload, and show us the filename that you're trying to upload? There might be a clue thereabouts.

Re: Upload a file from a web form
by antirice (Priest) on Jul 09, 2003 at 22:28 UTC

    Well bodmin, all of our help and it still isn't working. Of course, your responses aren't helping us help you either. My crystal ball is in the shop for repairs now and my mindreader is on vacation.* So until then, you need to offer more for us to go on. Are you getting any errors in your webserver logs? Is the webpage returning a 500 error? Are you certain that you have a field as such: <input type="file" name="uploaded_file">? You are running with use strict; use warnings; use diagnostics; right? Have you tried perl -c myscript.cgi? Does the file that you are uploading contain anything? We just need more information to figure out what is going on.

    * Sorry for the sarcasm. Your script should work if everything else is as I would assume it to be.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Upload a file from a web form
by submersible_toaster (Chaplain) on Jul 10, 2003 at 02:35 UTC

    Out of curiosity bodmin , are you in control of the httpd for this site, or have you been explicitly told that uploads are allowed. Something in httpd.conf may be taking effect before your CGI gets going.

    Only guessing but wouldn't something like..

    <Limit POST> Require some-acl-match </Limit>

    or who knows what else cause problems for you. I'm out of ideas


    I can't believe it's not psellchecked
Re: Upload a file from a web form
by Anonymous Monk on Jul 10, 2003 at 13:55 UTC
    We use a code like this to upload files from web forms:

    #!/usr/bin/perl # # Save a file uploaded using a web form # use CGI qw(:standard); # $attach1 = param(attach1); # $fh1 = $$attach1; # # Get the file name without any kind of slash # if($attach1 =~ /\\/){ my @filename_parts = split /\\/,$attach1; my $length = scalar(@filename_parts); $attach1 = $filename_parts[--$length]; } if($attach1 =~ /\//){ my @filename_parts = split /\//,$attach1; my $length = scalar(@filename_parts); $attach1 = $filename_parts[--$length]; } $directory = "/home/uploads"; open(ARQ,">","$directory/$attach1"); while(<$fh1>) { print ARQ "$_"; } close ARQ; exit;
    Hope this helps.

    Ron

Re: Upload a file from a web form
by CodeJunkie (Monk) on Jul 10, 2003 at 09:26 UTC

    This works for me, hope its some help to you.

    open (OUTPUT,">$outfile") || die "Cannot open image file: $!"; binmode(OUTPUT); my $buffer; while (my $bytesread = read($filename_thumbnail, $buffer, 4096)) { my $length += $bytesread; print OUTPUT $buffer || die "Cannot write the image to file: $!"; } close OUTPUT || die "Cannot close image file: $!"; my $koko = int(($length+512)/1024); my $maxsize="1024"; if($koko > $maxsize && $maxsize > 0) { unlink "$outfile"; print "File size is limited to ". $maxsize ."KB<br>$koko"; }

    Then the form should look like this

    <form name="additem" method="post" action="$ENV{'SCRIPT_NAME'} enctype +="multipart/form-data"> <input type="file" name="outfile" size="10" class="submit"> <input type="submit"> </form>

    I kinda missed out some usual CGI and perl bits there, but I hope that's some help to you.

    Cheers,
    Tom

      Just looking over the responses again, maybe your all important missing code is the

      binmode(OUTPUT);

      Just a thought... let me know if it helps any

        Wish it did help. But no, thanks all the same.