Questions regarding filehandler

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • casybay
    New Member
    • Oct 2007
    • 35

    #1

    Questions regarding filehandler

    Hi guys,
    I am handling some upload functions in perl. And I got a problem that I really don't know how to figure it out.
    Here is that. I have two files, basically they have the same code in the upload part. But one is working fine, one is not working. I got this error:

    binmode() on closed filehandle UPLOADFILE at /spare/WWW/cgi-bin/uploadLindegren Current.pl line 62, <$fh> line 1.
    readline() on unopened filehandle at /spare/WWW/cgi-bin/uploadLindegren Current.pl line 63.

    I compare the upload parts between these two files, I cannot tell the difference. Could you guys be so kind to give me a hand.

    Appreciate that!!!

    Here is the code:
    Code:
    ......
    
    use Tie::File;
    
    $upload_dir = "../uploadTest"; 
    
    $query = new CGI;
    
    $data_file="lindegrenChoice.cgi"; 
    
    open(DAT, $data_file) || die("Could not open file!"); 
    
    @raw_data=<DAT>; 
    
    tie @line, 'Tie::File', $data_file or die("Can not open file!"); 
    
    .......
    
    for($i=1; $i<=$count; $i++){  
       if($current eq $sname[$i]){
         $filename =~ s/.*[\/\\](.*)/$1/; 
         @exten = split('\.', $filename);  
         $filetype = ".";  
         $filetype .= $exten[1];  
         $newfilename = "current_lindegren";  
         $newfilename .= $filetype;  
         $upload_filehandle = $filename;  
         $line[$i-1]="$i|$sname[$i]|1|1|$filetype"; 
    
         open UPLOADFILE, ">$upload_dir/$newfilename"; 
         binmode UPLOADFILE;  
         while (  <$upload_filehandle> )      {   
         print UPLOADFILE;    
         }     
         close UPLOADFILE;   
        }
       else {
           if($scurrent[$i]==0){
             $line[$i-1]="$i|$sname[$i]|$sexist[$i]|0|$stype[$i]";
          ##   print "line $i-1 is $line[$i-1] <br>";
           }
           else{ 
             $line[$i-1]="$i|$sname[$i]|0|0|$stype[$i]";
           }
       }
     } 
    close(DAT);
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by casybay
    Hi guys,
    I am handling some upload functions in perl. And I got a problem that I really don't know how to figure it out.
    Here is that. I have two files, basically they have the same code in the upload part. But one is working fine, one is not working. I got this error:

    binmode() on closed filehandle UPLOADFILE at /spare/WWW/cgi-bin/uploadLindegren Current.pl line 62, <$fh> line 1.
    readline() on unopened filehandle at /spare/WWW/cgi-bin/uploadLindegren Current.pl line 63.

    I compare the upload parts between these two files, I cannot tell the difference. Could you guys be so kind to give me a hand.

    Appreciate that!!!

    Here is the code:
    Code:
    ......
    
    use Tie::File;
    
    $upload_dir = "../uploadTest"; 
    
    $query = new CGI;
    
    $data_file="lindegrenChoice.cgi"; 
    
    open(DAT, $data_file) || die("Could not open file!"); 
    
    @raw_data=<DAT>; 
    
    tie @line, 'Tie::File', $data_file or die("Can not open file!"); 
    
    .......
    
    for($i=1; $i<=$count; $i++){  
       if($current eq $sname[$i]){
         $filename =~ s/.*[\/\\](.*)/$1/; 
         @exten = split('\.', $filename);  
         $filetype = ".";  
         $filetype .= $exten[1];  
         $newfilename = "current_lindegren";  
         $newfilename .= $filetype;  
         $upload_filehandle = $filename;  
         $line[$i-1]="$i|$sname[$i]|1|1|$filetype"; 
    
         open UPLOADFILE, ">$upload_dir/$newfilename"; 
         binmode UPLOADFILE;  
         while (  <$upload_filehandle> )      {   
         print UPLOADFILE;    
         }     
         close UPLOADFILE;   
        }
       else {
           if($scurrent[$i]==0){
             $line[$i-1]="$i|$sname[$i]|$sexist[$i]|0|$stype[$i]";
          ##   print "line $i-1 is $line[$i-1] <br>";
           }
           else{ 
             $line[$i-1]="$i|$sname[$i]|0|0|$stype[$i]";
           }
       }
     } 
    close(DAT);
    I would check to ensure that the variable $newfilename is getting set properly by say, using a print statement on it before it is used. If it is a null variable, then the open() will fail, thus resulting in a closed file handle.

    Regards,

    Jeff

    Comment

    • casybay
      New Member
      • Oct 2007
      • 35

      #3
      Thanks for your prompt reply, Jeff. After upload, the new file does be created under the correct upload path, however, with size zero. And I check the error log, I got the errors that I listed above.
      Originally posted by numberwhun
      I would check to ensure that the variable $newfilename is getting set properly by say, using a print statement on it before it is used. If it is a null variable, then the open() will fail, thus resulting in a closed file handle.

      Regards,

      Jeff

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by casybay
        Thanks for your prompt reply, Jeff. After upload, the new file does be created under the correct upload path, however, with size zero. And I check the error log, I got the errors that I listed above.

        Ok, did you check the variable I mentioned with a print statement to ensure that it is getting set properly? Also, in your script, please be sure and set the pragma's "use strict" and "use warnings". These will help you iron out initial issues that are recurring in just about everyone's scripts, before you post here.

        Regards,

        Jeff

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          change this line:

          open UPLOADFILE, ">$upload_d ir/$newfilename";

          to:

          open UPLOADFILE, ">$upload_d ir/$newfilename" or die qq{Can't open "$upload_di r/$newfilename" : $!};

          Comment

          Working...