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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Code not working
by davorg (Chancellor) on Nov 09, 2006 at 09:16 UTC

    What behaviour are you seeing? Does the file open at all?

    Is Excel installed on the PC? Does the browser have the correct file associations set up?

    Perhaps you should look at including a "Content-disposition" header as well.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Code not working
by fenLisesi (Priest) on Nov 09, 2006 at 09:23 UTC
    I think you should read up on perlsec (Taint mode in particular). Otherwise you could try -attachment in header() and/or -type => "application/octet-stream". Are you sure that there is such a file under /tmp? Cheers.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Opening Excel from CGI Program
by Jenda (Abbot) on Nov 10, 2006 at 01:23 UTC

    Apart from whatever header trickery you had to make to force IE to do what you want there are three more problems with the code!

    First, you did not binmode() neither STDOUT nor NEWFILE. While this might possibly work well under a Unix it will definitely break under Windows and may well break under Unixes too with some system and perl settings. By default files are opened in "text" mode, which may mean conversions of line end characters, conversions to or from utf8 and so forth. If you are working with binary data, do use binmode(FILEHANDLE). Always.

    Second, you read the whole file into a list, splitting it on newlines (the file is binary, there is no notion of lines there, the newline characters inside are purely accidental!) and then print all elements of the array. This means that the amount of memory you need it proportional to the size of the file. That's not a good thing. You should read the file in chunks (4KB sounds like a reasonable size to me) and print the chunks.

    ... binmode(STDOUT); open(NEWFILE,"<", "/tmp/$filename") or report_problem("Can't open the +file: $^E"); binmode(NEWFILE); my $buff; while (sysread(NEWFILE,$buff, 4*1024)) { print $buff; } close(NEWFILE);

    Then there's another important problem. Security. Imagine someone sends your script conid=../etc/passwd%00. The filename parameter will be completely unimportant in this case since once the filename is passed to the system functions all the stuff after the \x00 character is ignored. This means that you'll dutifully send the hacker your /tmp/../etc/passwd, that is, your /etc/passwd. Erm ... probably not what you intended, right?

Re: Opening Excel from CGI Program
by derby (Abbot) on Nov 09, 2006 at 13:47 UTC

    open(NEWFILE,"</tmp/$filename"); print <NEWFILE>; close(NEWFILE);
    You're printing to a filehandle you've opened for reading. You want
    open(NEWFILE,"</tmp/$filename"); while(<NEWFILE>) { print $_; } close(NEWFILE);

    -derby

    Update: Doh!

      open(NEWFILE,"</tmp/$filename"); print <NEWFILE>; close(NEWFILE);

      You're printing to a filehandle you've opened for reading.

      Look again. The code is reading from NEWFILE and writing to STDOUT.

      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg