Mailing List Archive

File upload problem
I am attempting to implement an upload script and having problems. I see from the apache::asp documentation (http://www.apache-asp.org/cgi.html) that the recommendation is to roll back to CGI v2.78. This is not an option in my case- I am locked into 3.43. I am hoping that somebody can offer suggestions on patching either apache::asp or CGI to get my script to function. I am adapting the upload script from here, and it looks pretty straight forward:
http://www.sitepoint.com/article/uploading-files-cgi-perl/

Unfortunately, when I run the script, I end up with an empty file on my server. I have edited CGI.pm to use an appropriate $CGITempFile::TMPDIRECTORY

Some other potentially relevant specs: Fedora 10, Apache/2.2.11 (Unix) mod_perl/2.0.4 Perl/v5.10.0

Also, as another potential clue, I found which attempting to debug that I seem to have an empty request object... If I try to read directly from request->Form I get the error:

Can't locate object method "request" via package "Apache" at /home/httpd/html-MBA/Users/InputForms/Me
taManager4.html line 93. <--> , /usr/local/lib/perl5/site_perl/5.10.0/Apache/ASP.pm line 1522

Also, when I attempt to test the upload script from http://www.apache-asp.org/eg/file_upload.asp on my own server, I again seem to be stymied by the missing request object.

Many thanks for assisting in sorting this issue out.


Andrew Koebrick

++++++++++++++
Problem script:

my $safe_filename_characters = "a-zA-Z0-9_.-";
my $upload_dir = "$ENV{DOCUMENT_ROOT}/$cfg->{DocumentStore}";

my $filename = $query->param("Identifier-upload");

if ( !$filename )
{
print $query->header ( );
print "There was a problem uploading your photo (try a smaller file).";
exit;
}

my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );
$filename = $name . $extension;
$filename =~ tr/ /_/;
$filename =~ s/[^$safe_filename_characters]//g;

if ( $filename =~ /^([$safe_filename_characters]+)$/ )
{
$filename = $1;
}
else
{
die "Filename contains invalid characters";
}




my $upload_filehandle = $query->upload("Identifier-upload");
print "Location $upload_dir/$filename";
open ( UPLOADFILE, ">/home/httpd/html-MBA/$filename" ) or die "$!";
binmode UPLOADFILE;
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}

close UPLOADFILE;
Re: File upload problem [ In reply to ]
Andrew Koebrick (ADM) wrote:
> (http://www.apache-asp.org/cgi.html) that the recommendation is to roll
> back to CGI v2.78.

The docs are just giving that version as a known-working example. I've
successfully uploaded files to servers running CGI.pm v2.89 and v3.15.

> I end up with an empty file on my server.

Empty, or missing?

If you're expecting to access the actual file-on-disk, rather just read
the data via a file handle, you need to set

PerlSetVar FileUploadTemp 1

in your httpd.conf file. Otherwise, Apache deletes the temp file it
uses to hold the upload. As an example of why you'd care, my code sets
this configuration variable this because the first thing it does is 'mv'
the file to its new permanent home in the filesystem.

> If I try to read directly from
> request->Form I get the error:

Perl is case-sensitive, and the $ is not optional. It's
"$Request->Form". I typically do this at the top of pages that receive
form posts:

if ($Request->{Method} eq 'POST') {
my $form = $Request->Form;
# process $form->{stuff} here
}

Being able to access the form values via $form saves a lot of typing.

> my $filename = $query->param("Identifier-upload");

I would only use CGI.pm to set up the file upload form, as described in
the Apache::ASP docs. I wouldn't continue to use it on the POST
handling side like this. Apache::ASP has its own mechanisms, which I'd
trust more. Using the shorthand above, you'd say

my $filename = $form->{Identifier-upload};

instead.

> my $upload_filehandle = $query->upload("Identifier-upload");

Again, you're fighting Apache::ASP by doing things through CGI.pm that
Apache::ASP already does. If you want just the file handle, that's

$Request->{FileUpload}{upload_field}->{FileHandle}

...in ASP-speak. See http://www.apache-asp.org/cgi.html#File%20Upload

If you set the httpd.conf variable as above, the name of the file is in

$Request->{FileUpload}{mfile}->{TempFile}

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
RE: File upload problem / $Request->Form problem [ In reply to ]
Warren,

Thank you for the response.

I had actually tried this syntax:

if ($Request->{Method} eq 'POST') {
my $form = $Request->Form;
# process $form->{stuff} here
}

But I can not seem to access anything from the form via this method. In fact there seems to be little I can get from the Request object. If I submit my form and try:
print "Request total bytes $Request->{TotalBytes}<br>";
print "Request method $Request->{Method}<br>";

I get:
Request total bytes 0
Request method POST

Also when I try to grab any form content via the construct you suggest, I get no content. For example, in my form I have a required field 'Title'. Testing access to my fields using both CGI and the native asp methods:

my $form = $Request->Form;
my $title = $form->{'Title'};
my $title2 = $query->param("Title");
print "asp title: $title<br>";
print "cgi title: $title2<br>";

results in

asp title:
cgi title: test title


Any ideas on what could be botching or interfering with the Request object?

Andrew Koebrick

-----Original Message-----
From: Warren Young [mailto:warren@etr-usa.com]
Sent: Tuesday, June 30, 2009 8:42 PM
To: Apache-ASP List
Subject: Re: File upload problem

Andrew Koebrick (ADM) wrote:
> (http://www.apache-asp.org/cgi.html) that the recommendation is to roll
> back to CGI v2.78.

The docs are just giving that version as a known-working example. I've
successfully uploaded files to servers running CGI.pm v2.89 and v3.15.

> I end up with an empty file on my server.

Empty, or missing?

If you're expecting to access the actual file-on-disk, rather just read
the data via a file handle, you need to set

PerlSetVar FileUploadTemp 1

in your httpd.conf file. Otherwise, Apache deletes the temp file it
uses to hold the upload. As an example of why you'd care, my code sets
this configuration variable this because the first thing it does is 'mv'
the file to its new permanent home in the filesystem.

> If I try to read directly from
> request->Form I get the error:

Perl is case-sensitive, and the $ is not optional. It's
"$Request->Form". I typically do this at the top of pages that receive
form posts:

if ($Request->{Method} eq 'POST') {
my $form = $Request->Form;
# process $form->{stuff} here
}

Being able to access the form values via $form saves a lot of typing.

> my $filename = $query->param("Identifier-upload");

I would only use CGI.pm to set up the file upload form, as described in
the Apache::ASP docs. I wouldn't continue to use it on the POST
handling side like this. Apache::ASP has its own mechanisms, which I'd
trust more. Using the shorthand above, you'd say

my $filename = $form->{Identifier-upload};

instead.

> my $upload_filehandle = $query->upload("Identifier-upload");

Again, you're fighting Apache::ASP by doing things through CGI.pm that
Apache::ASP already does. If you want just the file handle, that's

$Request->{FileUpload}{upload_field}->{FileHandle}

...in ASP-speak. See http://www.apache-asp.org/cgi.html#File%20Upload

If you set the httpd.conf variable as above, the name of the file is in

$Request->{FileUpload}{mfile}->{TempFile}

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
Re: File upload problem / $Request->Form problem [ In reply to ]
Andrew Koebrick (ADM) wrote:
>
> asp title:
> cgi title: test title

Would I be correct in guessing that this Apache::ASP application is
brand new, and hasn't been running successfully for some time in the
same configuration? If so, I would guess that you don't yet have
Apache::ASP set up correctly. Try the examples, and the generic
troubleshooting steps you can find in the mailling list archives and the
docs.

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
RE: File upload problem [ In reply to ]
>> Would I be correct in guessing that this Apache::ASP application is
brand new..?

Well, there have been the usual updates to the perl modules, but I have been running apache::asp on the site since ~2000. But as I generally only use apache::asp to embed the perl, rather than to replace the CGI functionality, it could be that the problem is longer lasting. I have access data from $Request->Form before, but not for a long while.

I am assuming the problem related to how apache::asp and mod_perl are interacting on my system. For a few asp versions, I had to patch ASP.pm to get it to run at all (ala this advice: http://www.gossamer-threads.com/lists/modperl/asp/82238; basically testing for correct mod_perl version). But with the latest version of the module, that did not seem to be necessary. I suspect it is this because of the error I mentioned in my first note:

Can't locate object method "request" via package "Apache" at /home/httpd/html-MBA/Users/InputForms/MetaManager4.html line 93. <--> , /usr/local/lib/perl5/site_perl/5.10.0/Apache/ASP.pm line 1522

Anyhow, I will keep digging; perhaps try a reinstall. Thanks for you assistance.

Andrew

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
Re: File upload problem [ In reply to ]
Andrew Koebrick (ADM) wrote:
> Can't locate object method "request" via package "Apache"

Have you tried this patch:

http://www.gossamer-threads.com/lists/modperl/asp/89060?search_string=yarrow;#89060

?

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
Re: File upload problem / $Request->Form problem [ In reply to ]
Probably not what is causing your scenario but...

I once had a scenario similar to this and found the problem was in my
HTML. I needed to use both "id" and "name" attributes for each element
in the form. If I just used one or the other, it wouldn't pass any data
for that element.


Andrew Koebrick (ADM) wrote:

> Any ideas on what could be botching or interfering with the Request object?


---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
RE: File upload problem [ In reply to ]
It looks like I figured out my problem with the missing $Request->Form data. I have RequestBinaryRead set to "Off". Turning this off gives me content in $Reqest->Form, but it unfortunately seems to then break CGI.pm.

I had ReqestBinaryRead turned off because of a different problem I was having back in 2004 with POST data truncation:
http://www.mail-archive.com/asp@perl.apache.org/msg02159.html


So it looks like at this point CGI.pm and Apache::ASP are at odds. Any thoughts on getting them to play well together?

Andrew Koebrick


---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org
Re: File upload problem [ In reply to ]
Andrew Koebrick (ADM) wrote:
> Any thoughts on getting them to play well together?

The use of CGI.pm to construct file upload fields is a mere convenience.
You can do the same thing by writing raw HTML. It's just uglier than
the corresponding CGI.pm code, is all.

As I said above, all other uses of CGI.pm should have equivalents in
Apache::ASP, so there's really no good reason to use CGI.pm.

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org