help with upload perl

N

News sender

Hi there,

I am using CGI and Perl for uploading files. The Perl code is included.

The code works fine on PC and Linux (both run apache 2.0.39). But when I run
the code on Unix (solaris7, also run Apache2.0.39), only the filename is
uploaded, not the contents:

-rw------- 1 nobody nobody 0 Apr 19 16:10 file1
-rw------- 1 nobody nobody 0 Apr 19 16:35 file2

You can see the file names are right: file1, file2. But the size of each
file is 0.

The permission of the directory is set as 777:
drwxrwxrwx 2 nobody nobody 512 Apr 2 15:33 shared

There is no error messages in the Apache log files.

Does anyone see this problem before? Any help is appreciated.

Many thanks.

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

use CGI;

$query = new CGI;

$filepath = $query->param ('filename');

if ($filepath =~ /([^\/\\]+)$/)
{
$filename="$1";
print "The file name is: ", $filename, "<br>";
}

else
{
$filename="$filepath";
}

# check the existence of the file
if (-e "../htdocs/sharedfiles/$filename")
{
print "A file with the same name exists, please rename your file.",
"<br>";
}
else
{
# open the file and load it
open (OUTFILE,">../htdocs/sharedfiles/$filename ");
binmode(OUTFILE);
while ($bytesread=read($filepath,$buffer,1024)) {
print OUTFILE $buffer;
}
close OUTFILE;
print "Your file has been successfully uploaded.", "<br>";
}
 
C

Clyde Ingram

Dear renping.liu,

The code works fine on PC and Linux (both run apache 2.0.39). But when I run
the code on Unix (solaris7, also run Apache2.0.39), only the filename is
uploaded, not the contents:

-rw------- 1 nobody nobody 0 Apr 19 16:10 file1
-rw------- 1 nobody nobody 0 Apr 19 16:35 file2

You can see the file names are right: file1, file2. But the size of each
file is 0.

Have a look at http://stein.cshl.org/WWW/software/CGI/
Lincoln starts the section "USING the File Upload Feature" by warning:

"The file upload feature doesn't work with every combination of browser and
server. The various versions of Netscape and Internet Explorer on the
Macintosh, Unix and Windows platforms don't all seem to implement file
uploading in exactly the same way. I've tried to make CGI.pm work with all
versions on all platforms, but I keep getting reports from people of
instances that break the file upload feature. "

In the "Frequent Problems" sub-section, look at the problem entitled "You
can read the name of the uploaded file, but can't retrieve the data":

"First check that you've told CGI.pm to use the new multipart/form-data
scheme. If it still isn't working, there may be a problem with the temporary
files that CGI.pm needs to create in order to read in the (potentially very
large) uploaded files. Internally, CGI.pm tries to create temporary files
with names similar to CGITemp123456 in a temporary directory. To find a
suitable directory it first looks for /usr/tmp and then for /tmp. If it
can't find either of these directories, it tries for the current directory,
which is usually the same directory that the script resides in."

In the section "Creating a File Upload field", Lincoln says:

"In order to take full advantage of the file upload facility you must use
the new multipart form encoding scheme. You can do this either by calling
startform() and specify an encoding type of $CGI::MULTIPART or by using the
new start_multipart_form() method. If you don't use multipart encoding, then
you'll be able to retreive the name of the file selected by the remote user,
but you won't be able to access its contents."

He offers a very detailed analysis of problems and "best practice", in
particular:
1. "use strict;"
You forgot to put this at the top of your program. You also forgot to
enable warnings:
#!/usr/bin/perl -w
use strict;

2. "To be safe, use the upload() function"

So, instead of doing this:
while ($bytesread=read($filepath,$buffer,1024)) {

Try something like this:

1. Form Generation phase:

print
header(),
start_html(),
start_multipart_form( -name => 'myform' ),
filefield( -name => 'filename', 'maxlength' => 64, override => '1' ),
submit( -name 'Upload file', 'value' =. 'upload' ),
end_form(),
end_html();

2. Handling Form submission:

my $fh = upload( 'filename' );
while ( read $fh, $buffer, 1024 ) {
print OUTFILE, $buffer;
}

I have used code like this in a CGI script on Solaris 2.6, with Perl 5.004
(!!) and CGI.pm 2.76 with no problems.
(I was uploading text files, so did not need binmode.)

I hope this helps.
Regards,

Clyde
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top