Help On File Download Script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajiv07
    New Member
    • Jun 2007
    • 141

    Help On File Download Script

    Hi To all, I have a script for downloading file from the server.The Problem is when i try to download WMA file it get download but it is not playing.when i try to play this file in Windows Media Player It gives the following error

    Windows Media Player cannot play the file. The file is either corrupt or the Player does not support the format you are trying to play.


    Here is My script

    [CODE=Perl]#!/usr/bin/perl
    use CGI;
    use CGI::Carp qw(fatalsToBrow ser);

    my $cgi=new CGI;
    my $files_location ;
    my $ID;
    my @fileholder;

    my $files_location = "c:/perl/";

    my $cFile = $cgi->param('file' );

    if ($cFile eq '') {
    print "Content-type: text/html\n\n";
    print "You must specify a file to download.";
    } else {


    my $cFile=$files_l ocation.$cFile;



    open(DLFILE, "<$cFile") || die "cannot open the File $! $cFile";
    @fileholder = <DLFILE>;
    close (DLFILE) || Error ('close', 'file');

    print "Content-Type:applicatio n/x-download\n";
    print "Content-Disposition:att achment;filenam e=$cFile\n\n";
    print @fileholder
    }[/CODE]

    Is any chance the file get corrupt while downloading Or the Script is not efficient one.

    Please Help On this.

    Thank In advance.

    Regards
    RajivGandhi
  • prn
    Recognized Expert Contributor
    • Apr 2007
    • 254

    #2
    Hi Rajiv,

    It is true that your script is not particularly efficient, but that is not the problem. It is also not the case that the file is corrupted during downloading.

    The problem appears to be that your lines 24-30 open the file from your server, save the file as an array of lines of text and then print the file as lines of text to the output. Unfortunately, your file is not supposed to be treated as text.

    I suspect that what you ought to do is found in the Perl FAQs.

    Let us know if this isn't the problem.

    Best Regards,
    Paul

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Your script is potentially a security risk. You perform no validation on the filename passed to the script:

      my $cFile = $cgi->param('file' );

      A malicious user could try and pass in the path to a sensitive file and see if your script will blindly download it. The code you posted is probably not the final version, but I thought I would still mention the potential security risk in case you are not aware of it.

      Comment

      • rajiv07
        New Member
        • Jun 2007
        • 141

        #4
        Thanks Prn and Kevin,

        could you please suggest me any tutorial to develop a exact script for file download.

        Thanks.

        Regards
        RajibGandhi.

        Comment

        • rajiv07
          New Member
          • Jun 2007
          • 141

          #5
          Originally posted by prn
          Hi Rajiv,

          It is true that your script is not particularly efficient, but that is not the problem. It is also not the case that the file is corrupted during downloading.

          The problem appears to be that your lines 24-30 open the file from your server, save the file as an array of lines of text and then print the file as lines of text to the output. Unfortunately, your file is not supposed to be treated as text.

          I suspect that what you ought to do is found in the Perl FAQs.

          Let us know if this isn't the problem.

          Best Regards,
          Paul
          Thank u So Much Prn,

          Its working fine after adding this line before i read the file content.

          [CODE=Perl]binmode( DLFILE );[/CODE]

          The Conclusion
          On some systems (in general, DOS and Windows-based systems) binmode() is necessary when you're not working with a text file.

          Thank U

          Regards
          RajivGandhi

          Comment

          Working...