Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > CGI Upload Problems

Reply
Thread Tools

CGI Upload Problems

 
 
Hal Vaughan
Guest
Posts: n/a
 
      05-02-2006
I've written Perl scripts to upload files before, but I'm having a lot of
problems getting it to work in this case. I have a form with nothing
outstanding about it and I'm simply trying to use the CGI:aram(filename)
to get the handle so I can read from it and write it to a file on my web
server.

I've snipped the other parts, since the form and Perl code would be long.
Here's the basic form:

<form method="post" action="/perl/upload.pl" name="DataUpload">
UploadData: <input name="uploaddata" type="file">
<input name="UploadData" value="Submit" align="left" type="submit">
</form>

And here's what I have in my upload.pl script:
(again, the relevant parts, only)

use CGI;
$fname = CGI:aram("file");
$fdata = CGI:aram("data");
# $uhandle = CGI:aram("uploaddata");
$uhandle = CGI::upload("uploaddata");
$tfile = "/var/www/data/temp.upload";
open OUT, ">$tfile";
binmode OUT;
$fdata = <$uhandle> or cgilog("DEBUG Upload Error: $!");
print OUT $fdata;
close OUT;

At this point I'm only testing this with a text file that is less than 50
bytes long, so I'm not worried about reading in too much data at once.
Once I've got it working, I'll replace that with a loop to read from the
file handle with a specified buffer length and write to the output.

cgilog() is my own logging routine that just dumps the log statements to a
log file. In this case, I keep getting either "No such file or directory"
as the error or no error reported at all. I also have uncommented the
commented out line and used that to replace the line above it. It makes no
difference.

My guess is this is something extremely simple and obvious that I expect to
be doing that I'm not because I've gone over this many times and spent
hours on Google to try to find what I'm doing wrong, but I can't find a
thing.

This is for a system running Apache (1.3) on Linux.

Thanks for any help.

Hal
 
Reply With Quote
 
 
 
 
Hal Vaughan
Guest
Posts: n/a
 
      05-02-2006
Forgot to include with this that it is on a secure server, using an HTTPS
page instead of HTTP. I don't know if that makes a difference.

Hal Vaughan wrote:

> I've written Perl scripts to upload files before, but I'm having a lot of
> problems getting it to work in this case. I have a form with nothing
> outstanding about it and I'm simply trying to use the CGI:aram(filename)
> to get the handle so I can read from it and write it to a file on my web
> server.
>
> I've snipped the other parts, since the form and Perl code would be long.
> Here's the basic form:
>
> <form method="post" action="/perl/upload.pl" name="DataUpload">
> UploadData: <input name="uploaddata" type="file">
> <input name="UploadData" value="Submit" align="left" type="submit">
> </form>
>
> And here's what I have in my upload.pl script:
> (again, the relevant parts, only)
>
> use CGI;
> $fname = CGI:aram("file");
> $fdata = CGI:aram("data");
> # $uhandle = CGI:aram("uploaddata");
> $uhandle = CGI::upload("uploaddata");
> $tfile = "/var/www/data/temp.upload";
> open OUT, ">$tfile";
> binmode OUT;
> $fdata = <$uhandle> or cgilog("DEBUG Upload Error: $!");
> print OUT $fdata;
> close OUT;
>
> At this point I'm only testing this with a text file that is less than 50
> bytes long, so I'm not worried about reading in too much data at once.
> Once I've got it working, I'll replace that with a loop to read from the
> file handle with a specified buffer length and write to the output.
>
> cgilog() is my own logging routine that just dumps the log statements to a
> log file. In this case, I keep getting either "No such file or directory"
> as the error or no error reported at all. I also have uncommented the
> commented out line and used that to replace the line above it. It makes
> no difference.
>
> My guess is this is something extremely simple and obvious that I expect
> to be doing that I'm not because I've gone over this many times and spent
> hours on Google to try to find what I'm doing wrong, but I can't find a
> thing.
>
> This is for a system running Apache (1.3) on Linux.
>
> Thanks for any help.
>
> Hal


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-02-2006
Hal Vaughan wrote:
> I've written Perl scripts to upload files before, but I'm having a lot of
> problems getting it to work in this case. I have a form with nothing
> outstanding about it and I'm simply trying to use the CGI:aram(filename)
> to get the handle so I can read from it and write it to a file on my web
> server.
>
> I've snipped the other parts, since the form and Perl code would be long.
> Here's the basic form:
>
> <form method="post" action="/perl/upload.pl" name="DataUpload">


You forgot the critical component:
enctype="multipart/form-data"

That attribute *must* be in your form tag for any file upload fields to
work.

(This, of course, has nothing to do with Perl, and everything to do
with CGI)

Paul Lalli

 
Reply With Quote
 
Hal Vaughan
Guest
Posts: n/a
 
      05-02-2006
Paul Lalli wrote:

> Hal Vaughan wrote:
>> I've written Perl scripts to upload files before, but I'm having a lot of
>> problems getting it to work in this case. I have a form with nothing
>> outstanding about it and I'm simply trying to use the
>> CGI:aram(filename) to get the handle so I can read from it and write it
>> to a file on my web server.
>>
>> I've snipped the other parts, since the form and Perl code would be long.
>> Here's the basic form:
>>
>> <form method="post" action="/perl/upload.pl" name="DataUpload">

>
> You forgot the critical component:
> enctype="multipart/form-data"
>
> That attribute *must* be in your form tag for any file upload fields to
> work.
>
> (This, of course, has nothing to do with Perl, and everything to do
> with CGI)
>
> Paul Lalli


Ouch.

I knew it had to be something so obvious that I expected it and kept missing
it when I checked it over (and a symbol recognition learning disability
doesn't help!).

I added this and now it works on my test server on my LAN, but does not work
on the web server I rent space on. Other upload tests work on that server,
but not this one.

Thanks and I'll post when I find out why it's working on my LAN and not on
the Internet server.

Hal
 
Reply With Quote
 
Hal Vaughan
Guest
Posts: n/a
 
      05-02-2006
This just does not make sense, but it fixes it. Any insight would be
appreciated.

I had a sample working that I had cut and pasted from a web page. I changed
the example script enough so it was working with the form I was already
using. I got it working without much trouble. Then I started copying over
lines from the non-working script to the example script. It worked fine.
Then I reversed the process, and it never would work.

I finally noticed the only difference was that the non-working script had 2
things the other didn't: "use strict;" and variable declarations with two
"our (...);" statements. It turns out if I declare $uhandle with all the
other variables, it does not work. The full script is rather short, so I
was declaring any variables that were used in the main script as "our" and
variables in routines as "my". (This isn't my normal way, but, again, this
script is short).

If $uhandle is declared as "our", it won't upload the file. If I declare it
with "my" it'll work. I've never seen a problem like this. Could it
somehow be interfering with the namespace in CGI or something else? I can
make this work now by just declaring it with "my", but does anyone know why
that makes a difference?

This is on Perl 5.6.0. The local server is 5.8.?, which would explain why
it worked on the local server and not the Internet one.

Hal


Hal Vaughan wrote:

> I've written Perl scripts to upload files before, but I'm having a lot of
> problems getting it to work in this case. I have a form with nothing
> outstanding about it and I'm simply trying to use the CGI:aram(filename)
> to get the handle so I can read from it and write it to a file on my web
> server.
>
> I've snipped the other parts, since the form and Perl code would be long.
> Here's the basic form:
>
> <form method="post" action="/perl/upload.pl" name="DataUpload">
> UploadData: <input name="uploaddata" type="file">
> <input name="UploadData" value="Submit" align="left" type="submit">
> </form>
>
> And here's what I have in my upload.pl script:
> (again, the relevant parts, only)
>
> use CGI;
> $fname = CGI:aram("file");
> $fdata = CGI:aram("data");
> # $uhandle = CGI:aram("uploaddata");
> $uhandle = CGI::upload("uploaddata");
> $tfile = "/var/www/data/temp.upload";
> open OUT, ">$tfile";
> binmode OUT;
> $fdata = <$uhandle> or cgilog("DEBUG Upload Error: $!");
> print OUT $fdata;
> close OUT;
>
> At this point I'm only testing this with a text file that is less than 50
> bytes long, so I'm not worried about reading in too much data at once.
> Once I've got it working, I'll replace that with a loop to read from the
> file handle with a specified buffer length and write to the output.
>
> cgilog() is my own logging routine that just dumps the log statements to a
> log file. In this case, I keep getting either "No such file or directory"
> as the error or no error reported at all. I also have uncommented the
> commented out line and used that to replace the line above it. It makes
> no difference.
>
> My guess is this is something extremely simple and obvious that I expect
> to be doing that I'm not because I've gone over this many times and spent
> hours on Google to try to find what I'm doing wrong, but I can't find a
> thing.
>
> This is for a system running Apache (1.3) on Linux.
>
> Thanks for any help.
>
> Hal


 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-02-2006
Hal Vaughan <(E-Mail Removed)> wrote:
>
> I've snipped the other parts, since the form and Perl code would be long.
> Here's the basic form:
>
> <form method="post" action="/perl/upload.pl" name="DataUpload">
> UploadData: <input name="uploaddata" type="file">
> <input name="UploadData" value="Submit" align="left" type="submit">
> </form>
>
> And here's what I have in my upload.pl script:
> (again, the relevant parts, only)
>
> use CGI;


No use strict?

> $fname = CGI:aram("file");
> $fdata = CGI:aram("data");


What is the point of these two lines?

> # $uhandle = CGI:aram("uploaddata");
> $uhandle = CGI::upload("uploaddata");
> $tfile = "/var/www/data/temp.upload";
> open OUT, ">$tfile";


Always check the success of open.

> binmode OUT;
> $fdata = <$uhandle> or cgilog("DEBUG Upload Error: $!");


It is not an error for a file-handle read to return a false value.

> print OUT $fdata;
> close OUT;


Usually check the success of close.


> At this point I'm only testing this with a text file that is less than 50
> bytes long, so I'm not worried about reading in too much data at once.
> Once I've got it working, I'll replace that with a loop to read from the
> file handle with a specified buffer length and write to the output.


CGI.pm already did this for you once (and your web server may have already
done it a second time.) You might want to look into simply copying the
temporary file that already exists.

> cgilog() is my own logging routine that just dumps the log statements to
> a log file. In this case, I keep getting either "No such file or
> directory"


Since reading an false value from a file-handle is not an error, the value
of $! after doing that is not meaningful. It is just left over from
whatever was in $! from some unknown point in the past.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      05-02-2006
Hal Vaughan wrote:
> This just does not make sense, but it fixes it. Any insight would be
> appreciated.

[...]
> If $uhandle is declared as "our", it won't upload the file. If I declare it
> with "my" it'll work. I've never seen a problem like this. Could it
> somehow be interfering with the namespace in CGI or something else? I can
> make this work now by just declaring it with "my", but does anyone know why
> that makes a difference?
>
> This is on Perl 5.6.0. The local server is 5.8.?, which would explain why
> it worked on the local server and not the Internet one.


The answer, or at least a good clue, should be found in your Web
server's error log. Hint: perldoc -f our
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-02-2006
Paul Lalli wrote:
> Hal Vaughan wrote:
>>
>><form method="post" action="/perl/upload.pl" name="DataUpload">

>
> You forgot the critical component:
> enctype="multipart/form-data"
>
> That attribute *must* be in your form tag for any file upload fields to
> work.
>
> (This, of course, has nothing to do with Perl, and everything to do
> with CGI)


True. Nevertheless, by using the Perl module CGI::UploadEasy you get
help to identify that rather common mistake.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File upload from client application (non-form based upload) stuart@microsoft.com Python 1 11-25-2006 12:14 AM
CGI file upload - problems with file size... DJ Stunks Perl Misc 1 01-23-2006 05:35 PM
Upload a file without file Upload control - ASP.Net =?Utf-8?B?U2FyYXY=?= ASP .Net 3 08-03-2005 01:09 AM
How to detect sizeof upload in order to show status of browser file upload? Heather Fraser Java 0 07-05-2004 12:35 AM
Upload Excel with Macros using HTML File Upload Prakash ASP General 3 11-12-2003 04:26 AM



Advertisments