By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

Help On File Download Script

rajiv07
100+
P: 141
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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl  
  2. use CGI;
  3. use CGI::Carp qw(fatalsToBrowser); 
  4.  
  5. my $cgi=new CGI;
  6. my $files_location; 
  7. my $ID; 
  8. my @fileholder;
  9.  
  10. my $files_location = "c:/perl/";
  11.  
  12. my $cFile = $cgi->param('file');
  13.  
  14. if ($cFile eq '') { 
  15. print "Content-type: text/html\n\n"; 
  16. print "You must specify a file to download."; 
  17. } else {
  18.  
  19.  
  20. my $cFile=$files_location.$cFile;
  21.  
  22.  
  23.  
  24. open(DLFILE, "<$cFile") || die "cannot open the File $! $cFile"; 
  25. @fileholder = <DLFILE>; 
  26. close (DLFILE) || Error ('close', 'file'); 
  27.  
  28. print "Content-Type:application/x-download\n"; 
  29. print "Content-Disposition:attachment;filename=$cFile\n\n";
  30. print @fileholder
  31. }
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
Jan 17 '08 #1
Share this Question
Share on Google+
4 Replies


prn
Expert 100+
P: 254
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
Jan 17 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
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.
Jan 17 '08 #3

rajiv07
100+
P: 141
Thanks Prn and Kevin,

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

Thanks.

Regards
RajibGandhi.
Jan 18 '08 #4

rajiv07
100+
P: 141
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.

Expand|Select|Wrap|Line Numbers
  1. binmode( DLFILE );
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
Jan 18 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.