Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 465
  • Last Modified:

$query->param not working..

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
netdreams
Asked:
netdreams
  • 10
  • 5
  • 4
  • +2
1 Solution
 
ozoCommented:
How are you using $file1?
0
 
netdreamsAuthor Commented:
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
 
rj2Commented:
Try to add the lines below to your script

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



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SapaCommented:
> 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
 
netdreamsAuthor Commented:
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
 
PeeweeCommented:
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
 
netdreamsAuthor Commented:
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
 
SapaCommented:
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
 
PeeweeCommented:
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
 
netdreamsAuthor Commented:
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
 
PeeweeCommented:
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
 
netdreamsAuthor Commented:
Peewee,

I change it and check the dir.
But still the same problem
0
 
PeeweeCommented:
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
 
PeeweeCommented:
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
 
netdreamsAuthor Commented:
Peewee,

I change it and check the dir.
But still the same problem
0
 
netdreamsAuthor Commented:
Peewee,

I also change the name field..
but same problem
0
 
netdreamsAuthor Commented:
Peewee,

I also change the name field..
but same problem
0
 
SapaCommented:
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
 
rj2Commented:
Check that the directory you're writing to is writable by the user/group the web server run as.

0
 
netdreamsAuthor Commented:
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
 
SapaCommented:
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
 
netdreamsAuthor Commented:
I think my problem is solved now
Thanx!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 10
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now