in reply to CGI File Upload

I don't know if you are still around...

I think that you mean upload a file through a web browser and put it in a certain directory?

I am doing a similar thing so I found http://www.webmasterbase.com/article/474 however I can't get it to work quite right so someone else may help. I get zero sized files. and an error message in the logs.

readline() on unopened filehandle at /var/www/admin/upload.cgi line 27.
It seems that the follwing line is failing in my code

my $upload_filehandle = $query->upload("myfilename");
myfilename is the name of the
<input type=file name="myfilename" ENCTYPE="multipart/form-data">
This is the upload script
#!/usr/bin/perl

use strict;
use warnings;

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

my $upload_dir = '/var/vhosts/www.woodheap.org/admin/incoming';

my $query = new CGI;
my $filename = $query->param('myfilename');

print $filename;

$filename =~ s/.*\/\\(.*)/$1/;

print $filename;
chmod 0777, "$filename";
my $upload_filehandle = $query->upload("myfilename");

open(UPLOADFILE, ">$upload_dir/$filename") or die $!;
chmod 0777, "$upload_dir/$filename";

binmode UPLOADFILE;

while ( <$upload_filehandle> )
{
    print UPLOADFILE;
}
close UPLOADFILE;
print $query->header ( );

Replies are listed 'Best First'.
Re: Re: CGI File Upload
by BrowserUk (Patriarch) on Sep 09, 2002 at 02:45 UTC

    Try changing line 27 (which by my count is while(<$upload_filehandle) to while(<UPLOADFILE>) and see if that helps any?


    Well It's better than the Abottoire, but Yorkshire!

      Sorry BrowserUK! Only just saw this post. I don't think that is the problem

      while <$upload_filehandle>
      is supposed to read the input. It is initialised with
      my $upload_filehandle = $query->upload("myfilename");
      On the otherhand,
      UPLOADFILE
      is supposed to be the output of the script: a copy of the uploaded file on the hard disk. I agree that the naming of my filehandles could have been much better