Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

very small upload script problem

Status
Not open for further replies.

madcgimonk

Programmer
Oct 8, 2004
25
US
The upload script does upload the file, but it's alwaos 0k in size. For some reason it's uploading the file name but not the file. You notice that when you're uploading via the form, it takes a fraction of a second for it to say the file was uploaded.. so it's not even trying to upload data.

Can anyone see what the problem might be?

Code:
if (param("UploadFile"))
{
   ## collected from form field (the file itself)
   my $remotefile = param('upload');
   my $filename = $remotefile;
   
   ## collected from form field (username)
   my $user     = param("select");
   
   ## strip off directory paths
   $filename =~ s/^.*[\\\/]//;

   my $localfile = "/home2/spyders/public_html/member/$user/$filename";
   # full file path to upload directory (must include $filename)


  #############
  # Check to see if the file already exists
  #############
   if(-e "$localfile") 
   {
      print "<center><font color=red>ERROR: A file with that filename already exists.  Upload aborted.</font></center>";
      exit;
   }

   ####    
   # open a new file and transfer bit by bit from what's in the buffer
   ####
    open( SAVED, ">>$localfile" );
  
    my $buffer;
    while ( my $bytesread = read( $remotefile, $buffer, 1024 ) ) 
    {      
        print SAVED $buffer;
    }
    close SAVED;

    my $mode = 0755;
    chmod $mode, "$localfile";

    print "<center><font color=blue>File was uploaded to <b>$user</b> successfully.</font></center>";

}
 
In your form, do you have:
Code:
<FORM action="../cgi-bin/whatever.cgi" method="post"
enctype="multipart/form-data">

With attention to the "enctype". I hope this helps.

X
 
Hi.

I didn't have enctype before and I added that. But it's still uploading 0k files.

This is what I have
Code:
<form action="" method="post" enctype="multipart/form-data">

Thanks.
 
There's a few FAQ's on uploading files, hopefully they'll answer your question

--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Also, you might want to add the line:

Code:
binmode SAVE;

to your script just before you read in the file. If the user sends a binary file, the way your script is now will corrupt the file upon upload.

- Rieekan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top