Uploading files using CGI.pm

D

D Borland

Hi folks,

Can anyone see why this is not working, when i use the script on my web
hosting service, it returns success but no image has been uploaded when i
look in the uploads dir, just can't see why it's not working...

__CODE__

#!/usr/bin/perl

use CGI;
use CGI::Carp 'fatalsToBrowser';
use File::Basename;

my $query = new CGI;

my $uploadDir = 'uploads';
my $uploadFile = $query->upload('image1');
my $filename = basename $query->param('image1');

open (NEWFILE), ">$uploadDir/$filename"
or die "Cannot open $uploadDir/$filename : $!\n";

binmode (NEWFILE);

while (<$uploadFile> ) {
print NEWFILE;
}

close (NEWFILE);

print $query->header,
$query->start_html('File Uploaded'),
$query->p('Succesful upload'),
$query->end_html;

__END__

Thanks for your time

Dagmar
 
J

Juha Laiho

D Borland said:
Can anyone see why this is not working, when i use the script on my web
hosting service, it returns success but no image has been uploaded when i
look in the uploads dir, just can't see why it's not working...
....
my $uploadDir = 'uploads'; ....
open (NEWFILE), ">$uploadDir/$filename"
or die "Cannot open $uploadDir/$filename : $!\n";


This at least could be a source of confusion.

When running a program as CGI, there's no guarantee what would be
the working directory of the program when it is run -- and the
$uploadDir is considered to be relative to that unknown directory.

So, change the 'uploads' to some full path to a directory, such as
'/your/web/dir/uploads'.

Other than that, I couldn't find anything strange in the program;
you could help yourself by printing out some debug information
to the browser as the CGI makes progress. So, print the header
right in the start of the program, then at various points print
out values of variables, or just status information from which
you know which parts of your program are being run.

I also suggest that you turn on warnings and strict -- even
though your code seems clean enough.
 
J

James Willmore

Can anyone see why this is not working, when i use the script on my
web hosting service, it returns success but no image has been
uploaded when i look in the uploads dir, just can't see why it's not
working...
<snip>

use strict should be in your code.

Have you checked the error log for the server? Are you sure you can
upload files? Some providers restrict that type of action. How about
the directory you're uploading to - have permission to place files
there? Also consider full paths instead of relative paths. The
server error log will aid you in determining if this is the issue.

There isn't much to go on - because you need to follow some of the
suggestions above and also need to consider posting to a CGI authoring
group.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Indifference will be the downfall of mankind, but who cares?
 
D

D Borland

It's OK i managed to figure it all out in the end.

Below is a copy of the script which now works fine.

Thanks for everyones time in this matter.

Dagmar

__CODE__

#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp 'fatalsToBrowser';

CGI::pOST_MAX = 1024 * 500;

my $uploadDir = "$ENV{'DOCUMENT_ROOT'}";

my $query = new CGI;

my $filename = $query->param('image1');
$filename =~ s/.*[\/\\](.*)/$1/;
my $uploadFile = $query->upload('image1');

open (NEWFILE, ">$uploadDir/$filename")
or die "Unable to open $uploadDir/$filename : $!\n";

binmode (NEWFILE);

while (<$uploadFile> ) {
print (NEWFILE);
}

close (NEWFILE);

print $query->header,
$query->start_html('File Uploaded'),
$query->p('Succesful upload'),
$query->end_html;

__END__
 
A

Alan J. Flavell

use strict should be in your code.

good advice
Have you checked the error log for the server? Are you sure you can
upload files?

Did you look at the code which the hon Usenaut provided? Seems to me
that the open was for writing, it correctly tested the result of the
open, it would have reported the results of an error correctly.

Seems to me it probably successfully opened a file somewhere, and
uploaded the data to it. Just that it wasn't where the hon Usenaut
looked for it, due to failing to specify its absolute filepath and not
knowing where the current working directory was at the time (this is
_NOT_ regulated by the CGI spec).
... and also need to consider posting to a CGI authoring group.

Yup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top