asifk1981 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am trying to upload ms-word documents from windows machine to my unix box, using perl. What I do is , read the doc into a buffer and then print it onto to a file in the unix box. Even though this works fine with html and text files, I am seeing extra junk data uploaded, when I try uploading MS-WORD documents. The uploaded file contains lot of junk data in it. How do I make sure that only the content of the word document is read and printed onto the file in the unix server? Please shed some light on this..Thanks in advance.
  • Comment on Uploading ms-word docs onto unix box using PERL

Replies are listed 'Best First'.
Re: Uploading ms-word docs onto unix box using PERL
by Joost (Canon) on Jul 11, 2005 at 16:32 UTC
Re: Uploading ms-word docs onto unix box using PERL
by samtregar (Abbot) on Jul 11, 2005 at 19:18 UTC
    How are you determining that the Word file contains "extra junk"? Are you opening it with Word or just looking at in a text editor. I'm wondering if you're interpreting perfectly valid binary data in the Word file as "extra junk."

    For what's worth it seems unlikely that binmode() will help. It's a rare Unix that can't write binary data to disk without help. You're not running Redhat 8.0, are you?

    -sam

Re: Uploading ms-word docs onto unix box using PERL
by davidrw (Prior) on Jul 11, 2005 at 16:47 UTC
    y, as Joost said, bunmode is a likely culprit..

    But what method/protocol are you using (http/ftp/scp/nfs/etc)? Can we see code? If the unix box directory is available to the windows box (my best guess as to what you're doing), are you using File::Copy? That would avoid you needing to read/write yourself.
      The outline of the code that carries out the activity is
      $q = CGI::new(); my $filename = $q->param('docname'); binmode($filename); if (open(OUTPUT, ">$output") || openError ()) { $Data = read( $filename, $buffer, BUFFER_SIZE ); }
      Even with the introduction of the binmode($filename) statement , the problem persists..Please advice me....
        Ah -- CGI .. so that the client is windows and host unix doesn't really matter...
        anyways, i think you need to do binmode OUTPUT .. Read the entire (especially the very last part: Be sure to call binmode() on any handle that you create to write the uploaded file to disk.) "CREATING A FILE UPLOAD FIELD" section in the CGI docs. You may also be interested in the my $fh = $p->upload('docname') usage as well.