I have problems with download scripts, it's trying to print instead of saving the file

E

E Arredondo

Here's my cgi script :

---------------------------------------
#!/usr/bin/perl -w

use CGI;

$upload_dir = "/upload";
$query = new CGI;
$filename = $query->param("photo");
$claim = $query->param("claim");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("photo");
open UPLOADFILE, ">$upload_dir/$filename";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
print $query->header ( );
print <<END_HTML;

<HTML>
<HEAD>
<TITLE>Thanks!</TITLE>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>
<BODY>
<P>Thanks for uploading your photo!</P>
<P>Your Claim: $claim</P>
</BODY>
</HTML>

END_HTML
--------- cut here --------------------------------

And here's the log file :
binmode() on closed filehandle UPLOADFILE at
/usr/lib/apache/cgi-bin/upload.cgi
line 13.
print() on closed filehandle UPLOADFILE at
/usr/lib/apache/cgi-bin/upload.cgi li
ne 16, <fh00001C%3A\Graphics\crank.JPG> line 1.
print() on closed filehandle UPLOADFILE at
/usr/lib/apache/cgi-bin/upload.cgi li
ne 16, <fh00001C%3A\Graphics\crank.JPG> line 2

keeps going........all the way until

print() on closed filehandle UPLOADFILE at
/usr/lib/apache/cgi-bin/upload.cgi li
ne 16, <fh00001C%3A\Graphics\crank.JPG> line 433.
print() on closed filehandle UPLOADFILE at
/usr/lib/apache/cgi-bin/upload.cgi li
ne 16, <fh00001C%3A\Graphics\crank.JPG> line 434.
print() on closed filehandle UPLOADFILE at
/usr/lib/apache/cgi-bin/upload.cgi li
ne 16, <fh00001C%3A\Graphics\crank.JPG> line 435.
UX:lp: ERROR: No default destination.
TO FIX: You must identify which printer should
handle your request by naming it or a
class of printers (-d name) or by naming
a type of printer (-T type).
----------------------- cut here ------------------------------------

I can't find the file, so I'm assuming that it tried it to print , Am I
right ?

I'm running SCO OSR 507 (please don't flame me), and perl version 5.8.6EB

Thanks
 
M

michaelpgee

You should always check the return value of open(). Try:

open(UPLOADFILE, ">$upload_dir/$filename")
or die "Can't write to '$upload_dir/$filename': $!\n";

I bet the webserver is running as a user that doesn't have permission
to write to the specified directory.
 
T

Tad McClellan

E Arredondo said:
#!/usr/bin/perl -w

use CGI;


You are missing a line that should be included in all of your Perl programs:

use strict;

$filename = $query->param("photo");
$filename =~ s/.*[\/\\](.*)/$1/;


perldoc File::Basename

$upload_filehandle = $query->upload("photo");


That variable contains a *string*, not a filehandle...

open UPLOADFILE, ">$upload_dir/$filename";


You should always, yes *always*, check the return value from open():

open UPLOADFILE, ">$upload_dir/$filename" or
die "could not open '$upload_dir/$filename' $!";

while ( <$upload_filehandle> )


You need to put an _actual_ filehandle inside the input operator.

<HEAD>
<TITLE>Thanks!</TITLE>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>


That is a rather bizarre document structure...

And here's the log file :
binmode() on closed filehandle UPLOADFILE at


Your open() failed, but you didn't know it because you did
not check its return value.
 
P

Paul Lalli

Tad said:
That variable contains a *string*, not a filehandle...
From perldoc CGI:
==================
To be safe, use the upload() function (new in version 2.47). When
called with the name of an upload field, upload() returns a filehandle,
or undef if the parameter is not a valid filehandle.

$fh = upload('uploaded_file');
while (<$fh>) {
print;
}
===================

Paul Lalli
 
G

Gunnar Hjalmarsson

E said:
use CGI;

$upload_dir = "/upload";
$query = new CGI;
$filename = $query->param("photo");
$claim = $query->param("claim");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("photo");
open UPLOADFILE, ">$upload_dir/$filename";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
print $query->header ( );

You may want to use the CPAN module CGI::UploadEasy. The above part of
your script can be replaced with:

use CGI::UploadEasy;
my $ue = CGI::UploadEasy->new(-uploaddir => "/upload");
my $cgi = $ue->cgiobject;
my $claim = $cgi->param('claim');
print $cgi->header;

Now, as others have said, I doubt that you have a directory "/upload"
under the server root. Maybe you mean "$ENV{DOCUMENT_ROOT}/upload".
CGI::UploadEasy includes the necessary checks to find out.
 
G

Gunnar Hjalmarsson

Tad said:
E said:
$filename = $query->param("photo");
$filename =~ s/.*[\/\\](.*)/$1/;

perldoc File::Basename

Not for parsing the path that comes with a file upload request.
File::Basename doesn't have a clue about the platform the file was
uploaded from.
 
G

Gunnar Hjalmarsson

Gunnar said:
Tad said:
E said:
$filename = $query->param("photo");
$filename =~ s/.*[\/\\](.*)/$1/;

perldoc File::Basename

Not for parsing the path that comes with a file upload request.
File::Basename doesn't have a clue about the platform the file was
uploaded from.

CGI::UploadEasy uses this expression to remove the non-filename part of
the path:

s#.*[\]:\\/]##

Hopefully it's platform independent...
 
E

E Arredondo

You should always check the return value of open(). Try:

open(UPLOADFILE, ">$upload_dir/$filename")
or die "Can't write to '$upload_dir/$filename': $!\n";

I bet the webserver is running as a user that doesn't have permission
to write to the specified directory.

WOW, it's working now! I had the wrong path to the upload!! now I added
/www/docs/upload.


Thanks so much
 
E

E Arredondo

Tad McClellan said:
You are missing a line that should be included in all of your Perl
programs:

use strict;

I'm getting this on the log file after adding the strict command:

Global symbol "$query" requires explicit package name at
/usr/lib/apache/cgi-bin/upload.cgi line 11.
Global symbol "$upload_dir" requires explicit package name at
/usr/lib/apache/cgi-bin/upload.cgi line 13.
Global symbol "$filename" requires explicit package name at
/usr/lib/apache/cgi-bin/upload.cgi line 13.
Global symbol "$upload_dir" requires explicit package name at
/usr/lib/apache/cgi-bin/upload.cgi line 14.
Global symbol "$filename" requires explicit package name at
/usr/lib/apache/cgi-bin/upload.cgi line 14.
Global symbol "$upload_filehandle" requires explicit package name at
/usr/lib/apache/cgi-bin/upload.cgi line 16.
Global symbol "$query" requires explicit package name at
/usr/lib/apache/cgi-bin/upload.cgi line 22.
Global symbol "$claim" requires explicit package name at
/usr/lib/apache/cgi-bin/upload.cgi line 23.
Execution of /usr/lib/apache/cgi-bin/upload.cgi aborted due to compilation
errors.
[Fri Sep 30 11:57:15 2005] [error] [client 69.239.42.161] Premature end of
script headers: /usr/lib/apache/cgi-bin/upload.cgi

$filename = $query->param("photo");
$filename =~ s/.*[\/\\](.*)/$1/;


perldoc File::Basename

$upload_filehandle = $query->upload("photo");


That variable contains a *string*, not a filehandle...

open UPLOADFILE, ">$upload_dir/$filename";


You should always, yes *always*, check the return value from open():

open UPLOADFILE, ">$upload_dir/$filename" or
die "could not open '$upload_dir/$filename' $!";

while ( <$upload_filehandle> )


You need to put an _actual_ filehandle inside the input operator.

<HEAD>
<TITLE>Thanks!</TITLE>
<HEAD>
<TITLE>Thanks!</TITLE>
</HEAD>


That is a rather bizarre document structure...

I screwed up when copying and pasting from my ssh window. sorry

Your open() failed, but you didn't know it because you did
not check its return value.

I got it! Thanks!!! for your comments
 
E

E Arredondo

Gunnar Hjalmarsson said:
E said:
use CGI;

$upload_dir = "/upload";
$query = new CGI;
$filename = $query->param("photo");
$claim = $query->param("claim");
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("photo");
open UPLOADFILE, ">$upload_dir/$filename";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
print $query->header ( );

You may want to use the CPAN module CGI::UploadEasy. The above part of
your script can be replaced with:

use CGI::UploadEasy;
my $ue = CGI::UploadEasy->new(-uploaddir => "/upload");
my $cgi = $ue->cgiobject;
my $claim = $cgi->param('claim');
print $cgi->header;

Now, as others have said, I doubt that you have a directory "/upload"
under the server root. Maybe you mean "$ENV{DOCUMENT_ROOT}/upload".
CGI::UploadEasy includes the necessary checks to find out.

Thanks for your help, Indeed there was no /upload, I though wrong. Do you
know if the CPAN CGI:UploadEasy will let me download whole folders ? Or Is
there a way to do a whole folder download ?

Thanks
 
E

E Arredondo

You should always check the return value of open(). Try:

open(UPLOADFILE, ">$upload_dir/$filename")
or die "Can't write to '$upload_dir/$filename': $!\n";

I bet the webserver is running as a user that doesn't have permission
to write to the specified directory.

What code should I use it I want the script to email someone about the
upload ?

here's the idea :

print { $filename was uploaded | mail -s $filename henry }

Thanks?
 
G

Gunnar Hjalmarsson

E said:
What code should I use it I want the script to email someone about the
upload ?

here's the idea :

print { $filename was uploaded | mail -s $filename henry }

perldoc -q "send mail"

There are several other modules available; my personal favorite is
Mail::Sender.
 
G

Gunnar Hjalmarsson

E said:
Do you know if the CPAN CGI:UploadEasy will let me download whole
folders ?

Suppose you mean upload...

It sure will let you upload multiple files at once. One way would be to
include multiple file upload controls in the form, but then you'd need
to manually pick each file before the submission.

To _automatically_ have all the files in a folder uploaded, you would
need to do some pre-processing. It ought to be possible to grab a bunch
of filepaths and make the file upload request via the LWP family of modules.
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top