Solved

$query->param  not working..

Posted on 2002-03-12
22
456 Views
Last Modified: 2010-03-05
Hello,

I'm use a form for uploading a file:
<FORM ACTION="$script_url" ENCTYPE="multipart/form-data" METHOD="POST">
<INPUT TYPE="file" NAME="file1">
....
</FORM>

And the perlscript:
#!/usr/bin/perl
use CGI;
$query=new CGI;
$file1=$query->param('file1');
...

But I can't get the value from the file-field "file1" in $file1 ?

When I use the method "GET" in my form I can read the value with $query->param('file1') but then I can't upload because I need the "POST"-method for uploading...

Can someone tell me what's wrong?





0
Comment
Question by:netdreams
  • 10
  • 5
  • 4
  • +2
22 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 6857334
How are you using $file1?
0
 

Author Comment

by:netdreams
ID: 6857459
I'm using more fields then file1.. also normal text fields.

I'm using an other subroutine to upload $file1.. but I can't read the values from my form with $query->param. That's my main problem.

On my own server it's working fine. But on the server of my customer is't only working with the GET method. But that not the method for uploading files.


0
 
LVL 10

Expert Comment

by:rj2
ID: 6857554
Try to add the lines below to your script

$CGI::DISABLE_UPLOADS   = 0;
$CGI::POST_MAX          = 52428800;



0
 
LVL 5

Expert Comment

by:Sapa
ID: 6858927
> I'm using more fields then file1.. also normal text fields.

The if 'file1' field in form has type='file', then $file1 variable after $file1=$query->param('file1');
will have some "magick" property. In string context it
gives filename you have uploaded:

my $filename = "$file1";

And it also works as file descriptor:

$filename =~ s/^.*\///;  # Strip possible full path
open F, "> /my/outdir/$filename"; # Open destination
while(read($file1, my $buf, 1024)) { # Read from temporary $file1
  print F $buf; # Write to destination
}
close F;
0
 

Author Comment

by:netdreams
ID: 6859906
I know but.. I even can't get the value from the filename in the string $file1  (with the command $file1=$query->param('file1')

All the fields in the form (also the not file fields) are empty..

I only get the values with the GET method in my form.. but then I can't upload a file.
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6860222
netdreams,
there is a complete example here taken from the website:
http://www.oreilly.com/catalog/cgi2.

also:

within your html you can use <input type="file" name="myfile">.  This is excellent for selecting the file.


make sure you ammend the config details within the cgi script if you intend to use it..

let me know how it goes
regards
peewee

#!/usr/bin/perl -wT

use strict;
use CGI;
use Fcntl qw( :DEFAULT :flock );

use constant UPLOAD_DIR     => "/usr/local/apache/data/uploads";
use constant BUFFER_SIZE    => 16_384;
use constant MAX_FILE_SIZE  => 1_048_576;       # Limit each upload to 1 MB
use constant MAX_DIR_SIZE   => 100 * 1_048_576; # Limit total uploads to 100 MB
use constant MAX_OPEN_TRIES => 100;

$CGI::DISABLE_UPLOADS   = 0;
$CGI::POST_MAX          = MAX_FILE_SIZE;

my $q = new CGI;
$q->cgi_error and error( $q, "Error transferring file: " . $q->cgi_error );

my $file      = $q->param( "file" )     || error( $q, "No file received." );
my $filename  = $q->param( "filename" ) || error( $q, "No filename entered." );
my $fh        = $q->upload( "file" );
my $buffer    = "";

if ( dir_size( UPLOAD_DIR ) + $ENV{CONTENT_LENGTH} > MAX_DIR_SIZE ) {
    error( $q, "Upload directory is full." );
}

# Allow letters, digits, periods, underscores, dashes
# Convert anything else to an underscore
$filename =~ s/[^\w.-]/_/g;
if ( $filename =~ /^(\w[\w.-]*)/ ) {
    $filename = $1;
}
else {
    error( $q, "Invalid file name; files must start with a letter or number." );
}

# Open output file, making sure the name is unique
until ( sysopen OUTPUT, UPLOAD_DIR . "/$filename", O_CREAT | O_RDWR | O_EXCL ) {
    $filename =~ s/(\d*)(\.\w+)$/($1||0) + 1 . $2/e;
    $1 >= MAX_OPEN_TRIES and error( $q, "Unable to save your file." );
}

# This is necessary for non-Unix systems; does nothing on Unix
binmode $fh;
binmode OUTPUT;

# Write contents to output file
while ( read( $fh, $buffer, BUFFER_SIZE ) ) {
    print OUTPUT $buffer;
}

close OUTPUT;

print $q->header( "text/plain" ), "File received.";

sub dir_size {
    my $dir = shift;
    my $dir_size = 0;
   
    # Loop through files and sum the sizes; doesn't descend down subdirs
    opendir DIR, $dir or die "Unable to open $dir: $!";
    while ( readdir DIR ) {
        $dir_size += -s "$dir/$_";
    }
    return $dir_size;
}


sub error {
    my( $q, $reason ) = @_;
   
    print $q->header( "text/html" ),
          $q->start_html( "Error" ),
          $q->h1( "Error" ),
          $q->p( "Your upload was not procesed because the following error ",
                 "occured: " ),
          $q->p( $q->i( $reason ) ),
          $q->end_html;
    exit;
}
0
 

Author Comment

by:netdreams
ID: 6860548
Hi Peewee,

I make a the HTMLfile: http://woning.nens.nl/upload.html
And the script: http://woning.nens.nl/cgi-bin/db/upload.pl

First I get the error: Too late for "-T" option at engine.pl line 1.  

So I turned of the -T option

Then I get the error: Function CGI::Object::cgi_error does not exist at upload.pl line 17

So I comment that row.

Then I try to upload a file and get the error: No file received from you script.  (try it yourself http://woning.nens.nl/upload.html)

Looks like.. the post-method not works alright?



0
 
LVL 5

Expert Comment

by:Sapa
ID: 6860605
Can you post the output of command:

perl -V -MCGI -e 'print CGI->VERSION'

also, the info about your OS and HTTP server
can be helpful.
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6860610
netdreams,
have you ammended the variables in the cgi script?


change:
 <input type="file" name="myfile">
to:
<input type="file" name="file">

make sure:
use constant UPLOAD_DIR     => "/usr/local/apache/data/uploads";

exists and is writeable.

regards
peewee
0
 

Author Comment

by:netdreams
ID: 6860624
Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration:
  Platform:
    osname=linux, osvers=2.2.14-5.0, archname=i686-linux
    uname='linux comeonline.nl 2.2.14-5.0 #1 tue mar 7 21:07:39 est 2000 i686 unknown '
    config_args='-der'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=undef d_sfio=undef uselargefiles=define
    use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=undef
  Compiler:
    cc='cc', optimize='-O2', gccversion=egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)
    cppflags='-fno-strict-aliasing'
    ccflags ='-fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64'
    stdchar='char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lndbm -lgdbm -ldb -ldl -lm -lc -lposix -lcrypt
    libc=/lib/libc-2.1.3.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'


Characteristics of this binary (from libperl):
  Compile-time options: USE_LARGE_FILES
  Built under linux
  Compiled at Feb 28 2001 13:49:35
  @INC:
    /usr/local/lib/perl5/5.6.0/i686-linux
    /usr/local/lib/perl5/5.6.0
    /usr/local/lib/perl5/site_perl/5.6.0/i686-linux
    /usr/local/lib/perl5/site_perl/5.6.0
    /usr/local/lib/perl5/site_perl
    .
3.01


More info: http://woning.nens.nl/cgi-bin/perldiver.cgi

0
 
LVL 5

Expert Comment

by:Peewee
ID: 6860625
netdreams,
have you ammended the variables in the cgi script?


change:
 <input type="file" name="myfile">
to:
<input type="file" name="file">

make sure:
use constant UPLOAD_DIR     => "/usr/local/apache/data/uploads";

exists and is writeable.

regards
peewee
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:netdreams
ID: 6860632
Peewee,

I change it and check the dir.
But still the same problem
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6860636
netdreams,
have you ammended the variables in the cgi script?


change:
 <input type="file" name="myfile">
to:
<input type="file" name="file">

make sure:
use constant UPLOAD_DIR     => "/usr/local/apache/data/uploads";

exists and is writeable.

regards
peewee
0
 
LVL 5

Expert Comment

by:Peewee
ID: 6860723
netdreams,
there's one more non synchronised thing i can see:

change:
<INPUT TYPE="text" NAME="name">
to:
<INPUT TYPE="text" NAME="filename">


regards
peewee
0
 

Author Comment

by:netdreams
ID: 6860754
Peewee,

I change it and check the dir.
But still the same problem
0
 

Author Comment

by:netdreams
ID: 6860758
Peewee,

I also change the name field..
but same problem
0
 

Author Comment

by:netdreams
ID: 6860767
Peewee,

I also change the name field..
but same problem
0
 
LVL 5

Expert Comment

by:Sapa
ID: 6860955
The problem is in CGI.pm v3.01. It's absolutely new code,
rewritten from scratch and it has too many bugs and incompatibilities with previous version. Multipart code is broken in this version. Note, there is no CGI.pm-3.01 on CPAN, the last stable one is CGI.pm-2.80

Andrey
0
 
LVL 10

Expert Comment

by:rj2
ID: 6861056
Check that the directory you're writing to is writable by the user/group the web server run as.

0
 

Author Comment

by:netdreams
ID: 6861221
Sapa,

Is there a way to use the CGI.pm 2.8 version instead of version 3.01? Or have to downgrade the current version?

0
 
LVL 5

Accepted Solution

by:
Sapa earned 50 total points
ID: 6861314
I recommend to downgrade the current version. It's for module developers only in any case. But you can install another version (2.80) without uninstalation the current one, if you want. In this case, you will have to add path to new module in your script explicitly. For example:

$ perl -MCPAN -e shell
CPAN> o conf makepl_arg "LIB=/home/netdreams/myperl/lib \
____> INSTALLMAN1DIR=/home/netdreams/myperl/man/man1 \
____> INSTALLMAN3DIR=/home/netdreams/myperl/man/man3"
CPAN> force install L/LD/LDS/CGI.pm-2.80.tar.gz
CPAN> quit

and add line:

use lib '/home/netdreams/myperl';

into the top of your script.
0
 

Author Comment

by:netdreams
ID: 6861359
I think my problem is solved now
Thanx!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now