Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

CGI File Upload

by damian (Beadle)
on Apr 28, 2000 at 05:40 UTC ( [id://9522]=perlquestion: print w/replies, xml ) Need Help??

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

if i want to upload a file from web, how will i able to save that uploaded file to a certain directory.... is this possible without using any perl modules? thanks in advance.

Replies are listed 'Best First'.
Re: CGI File Upload
by blm (Hermit) on Sep 09, 2002 at 02:08 UTC

    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 ( );
    

      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
Re: CGI File Upload
by belg4mit (Prior) on Sep 09, 2002 at 02:37 UTC
    Of course there is, but it's ill-advised. Bite the bullet and use a module, CGI comes with perl itself so there's no reason not to use it (until you do use it, and can come up with a better reason than disliking the use command ;-)

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://9522]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found