简体   繁体   中英

Why does my Perl script halt if CGI module is used after reading from stdin on Windows?

I'm trying to implement a progress indicator for file uploads. Part1 and Part2 of the script run properly if executed separately. But if executed together the script halts at:

my $cg = new CGI();

The problem only occurs on an Windows server. What could be the reason?

#!C:\Perl\bin\perl.exe -w
use CGI;
$post_data_filename = "C:\\test\\postdata.txt";
$uploaded_filename = "C:\\test\\uploaded_file.txt"; 

#PART 1
# read and store the raw post data in a temporary file so that we can repeatedly
# look at size of this temporary file in order to implement a progress bar
open(TMP,">","$post_data_filename");
$len = $ENV{'CONTENT_LENGTH'};
read (STDIN ,$LINE, $len);
print TMP $LINE;
close (TMP);

#PART 2
#use a CGI instance to read the raw post data and extract the uploaded file from it
my $cg = new CGI();
open(STDIN,"$post_data_filename");
my $fh = $cg->upload('file[0]');
open($tmp_fh, ">$uploaded_filename");
while(<$fh>) {
    print $tmp_fh $_;
    }
close($tmp_fh);

print "Content-type: text/html\n\n";
print "Ready\n";

exit;

Try doing binmode(STDIN) before reading from it. I'm guessing you are ending up with fewer bytes than the content length says and that's causing CGI to mess up. You may need to also do the binmode after reopening STDIN.

Also, please check all your IO operations for success.

On windows a file can't be open for reading while another process has it open for writing.

And your upload meter won't work because you read the whole STDIN and then write it to TMP, so you go from 0% straight to 100%.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM