Forum Moderators: coopster & phranque

Message Too Old, No Replies

upload_file

0 Bytes Read

         

reachedu

2:34 am on May 14, 2003 (gmt 0)

10+ Year Member



I am trying to provide a mechanism for people to upload images to my website. I have found many examples of this and they all seem to have the same basic logic. When I try to implement it, it seem to do ok, except the read never actually gets the file. I have included the code and the results. Any help is greatly appreciated.

sub upload_file
{
my $bytes_read=0;
my $size='';
my $buff='';
my $start_time;
my $time_took;
my $filepath='';
my $filename='';
my $write_file='';
$g_upload_path = "../artpics/";
$g_overwrite = 1;
$filepath=$fields{"filename1"};
print "Filepath = $filepath <br>";

if ($filepath =~ /([^\/\\]+)$/)
{
$filename="$1";
}
else
{
$filename="$filepath";
}
# if there's any space in the filename, get rid of them
$filename =~ s/\s+//g;
$write_file="$g_upload_path" ."$filename";

print "Filename=$filename<br>\n";
print "Writefile= $write_file<br>\n";

if ($g_overwrite == 0)
{
if (-e $write_file)
{
print("File $filename exists, will not overwrite!");
return;
}
}

if (!open(WFD,">$write_file"))
{
print "Write Filename = ",$write_file;
print "Error opening file for writing. It's a permission problem. Make sure your web server has write permission to the upload directory";
return;
}
$start_time=time();
$size = 0;
# no strict 'refs';
# binmode ($filepath);
while ($bytes_read=read($filepath,$buff,1024))
{
$size += $bytes_read;
binmode WFD;
print WFD $buff;
print "OK<br>";
}

print "size= ", $size,"<br>\n";
close(WFD);

if ((stat $write_file)[7] <= 0)
{
unlink($write_file);
print "Could not upload file: $filename";
return;
}
else
{
$time_took=time()-$start_time;
}

}

The results are:

Filepath = C:\2001.jpg
Filename=2001.jpg
Writefile= ../artpics/2001.jpg
size= 0
File 2001.jpg of size 0 bytes is uploaded successfully!

I have tried variations of the commented out lines for binmode and no strict but no luck. I get a file created in the right place with no data in it.

BCMG_Scott

11:42 am on May 14, 2003 (gmt 0)

10+ Year Member



what is your HTML form code like? You need to have enctype="multipart/form-data" in your form tag or it will not upload data:

<form enctype="multipart/form-data" action="action.php" method="post">

Scott Geiger

reachedu

2:47 am on May 15, 2003 (gmt 0)

10+ Year Member



Oh yea...That made a difference.

Not having handled multipart form data, this change pretty much broke the rest of my perl script, but I will figure out how to put it back together.

I'm an old school c programmer that has been out of technology for awhile. I'm not html/cgi literate yet. I've been begging and learning as I go. What is a good resource to find on using forms with cgi/perl.

As an old school programmer, I want to know what is under the covers. I'm not looking for a lot of routines that others have written.

BCMG_Scott

6:55 pm on May 15, 2003 (gmt 0)

10+ Year Member



I'm an old school c programmer that has been out of technology for awhile. I'm not html/cgi literate yet. I've been begging and learning as I go. What is a good resource to find on using forms with cgi/perl.

Hmm, not really sure. I tend to either use the CPAN modules or just parse the data out in the code. I also frequent perldoc.com a lot to look up perl functions and man pages.

BTW, the reason I suspected multipart/form-data was that I have been burned by it so many times - eventually I do learn! Glad it's working now. :)

Scott