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

Bad file Descriptor

rajiv07
100+
P: 141
Hi i have script for upload file to server.when i execute this script it gives Bad file Descriptor error.could u anybody help on this.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use CGI;
  3. use CGI::Carp qw(fatalsToBrowser);
  4. use File::Basename;
  5. use diagnostics;
  6. $CGI::POST_MAX = 1024 * 1000; 
  7. $CGI::DISABLE_UPLOADS = 0;  
  8. my $query=new CGI;
  9.  
  10. my $file = $query->param("file") ;
  11. my $upload_dir = "../templates/";
  12.  
  13. my $filename_characters = 'a-zA-Z0-9_.-';
  14.  
  15. my $file = $query->param('file') ;
  16.  
  17. $file=~ s/(^\s+|\s+$)//;
  18.  
  19. my @array=();
  20.  
  21.  my ($filename,undef,$ext) = fileparse($file,qr{\..*});
  22.  
  23. $filename .= $ext;
  24.  
  25. $filename =~ tr/ /_/;
  26.  
  27. $filename =~ s/[^$filename_characters]//g;
  28.  
  29. if($filename =~ /^([$filename_characters]+)$/) {
  30.    $filename = $1;
  31. }
  32. else{
  33.    error("The filename is not valid. Filenames can only contain these characters: $filename_characters");
  34. }
  35.  
  36. ##I got the error in this line ##
  37.  
  38. my $upload_filehandle = $query->upload("file") or die $!;
  39.  
  40. open (UPLOADFILE, ">$upload_dir/$filename") or error($!);
  41. binmode UPLOADFILE;
  42. while ( <$upload_filehandle> ) {
  43.    print UPLOADFILE;
  44. }
  45. close UPLOADFILE;
  46. sub error {
  47.    print $query->header(),
  48.          $query->start_html(-title=>'Error'),
  49.          shift,
  50.          $query->end_html;
  51.    exit(0);
  52. }
Regards
Rajiv
May 28 '08 #1
Share this Question
Share on Google+
5 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Hi i have script for upload file to server.when i execute this script it gives Bad file Descriptor error.could u anybody help on this.

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use CGI;
  3. use CGI::Carp qw(fatalsToBrowser);
  4. use File::Basename;
  5. use diagnostics;
  6. $CGI::POST_MAX = 1024 * 1000; 
  7. $CGI::DISABLE_UPLOADS = 0;  
  8. my $query=new CGI;
  9.  
  10. my $file = $query->param("file") ;
  11. my $upload_dir = "../templates/";
  12.  
  13. my $filename_characters = 'a-zA-Z0-9_.-';
  14.  
  15. my $file = $query->param('file') ;
  16.  
  17. $file=~ s/(^\s+|\s+$)//;
  18.  
  19. my @array=();
  20.  
  21.  my ($filename,undef,$ext) = fileparse($file,qr{\..*});
  22.  
  23. $filename .= $ext;
  24.  
  25. $filename =~ tr/ /_/;
  26.  
  27. $filename =~ s/[^$filename_characters]//g;
  28.  
  29. if($filename =~ /^([$filename_characters]+)$/) {
  30.    $filename = $1;
  31. }
  32. else{
  33.    error("The filename is not valid. Filenames can only contain these characters: $filename_characters");
  34. }
  35.  
  36. ##I got the error in this line ##
  37.  
  38. my $upload_filehandle = $query->upload("file") or die $!;
  39.  
  40. open (UPLOADFILE, ">$upload_dir/$filename") or error($!);
  41. binmode UPLOADFILE;
  42. while ( <$upload_filehandle> ) {
  43.    print UPLOADFILE;
  44. }
  45. close UPLOADFILE;
  46. sub error {
  47.    print $query->header(),
  48.          $query->start_html(-title=>'Error'),
  49.          shift,
  50.          $query->end_html;
  51.    exit(0);
  52. }
Regards
Rajiv
In line 38, shouldn't "file" be "$file"?

Regards,

Jeff
May 28 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Change this line:

Expand|Select|Wrap|Line Numbers
  1. open (UPLOADFILE, ">$upload_dir/$filename") or error($!);
change to:

Expand|Select|Wrap|Line Numbers
  1. open (UPLOADFILE, ">$upload_dir$filename") or error($!);
you already have a trailing slash on the end of $upload_dir as defined in line number eleven of the code you posted.
May 28 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
In line 38, shouldn't "file" be "$file"?

Regards,

Jeff
Short answer is: no.
May 28 '08 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
Short answer is: no.
That's what I get for digging through tons of code all day. :)
May 28 '08 #5

rajiv07
100+
P: 141
Change this line:

Expand|Select|Wrap|Line Numbers
  1. open (UPLOADFILE, ">$upload_dir/$filename") or error($!);
change to:

Expand|Select|Wrap|Line Numbers
  1. open (UPLOADFILE, ">$upload_dir$filename") or error($!);
you already have a trailing slash on the end of $upload_dir as defined in line number eleven of the code you posted.
Thanks Kevin.Its working fine.I have spent 1 hour to sort this but i couldn't find the small mistake.

Regards
Rajiv
May 29 '08 #6

Post your reply

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