I would suspect popen() happends to output STDERR, and that dir returns some STDERR when no files are found, or when the argument is invalid.. And if so, the STDERR message would get output directly to the webserver, and if it contains two newlines (which it does really I think), the webserver wouldn't get a complete set of headers (which it's complaining about)... Add this at the end of your command: 2 > &1, like this: $ListDir = "dir $Dir\\*.$pattern 2>&1"; Also, I think this would be even better: $ListDir = "dir \"$Dir\\*.$pattern\" 2>&1"; Happy coding! -- // DvDmanDT MSN: dvdmandt¤hotmail.com Mail: dvdmandt¤telia.com "B.A.T. Svensson" <B.A.T.Svensson@xxxxxxx> skrev i meddelandet news:1079951719.30259.138.camel@xxxxxxxx > I am curious about why the following error: > > > "The specified CGI application misbehaved by not returning a complete > set of HTTP headers." > > is generated while trying to read data from an empty pipe. > > > > ********************************************************** > > DISCUSSION: > > I have an (working) application that renders an out put in > side a table based on the menu selection the user does. > > Basically the web pages is organized as such: > > +-----------+ > | PAGE HEAD | > +-+---------+ > |M| | > |E| OUTPUT | > |N| VIEW | > |U| | > . . . > . . . > . . . > +-+---------+ > | PAGE FOOT | > +-----------+ > > > > Now, if I select a certain menu choices, and the system is in a > certain state (will soon explain this) then the CGI error "The > specified CGI application misbehaved by not returning a complete > set of HTTP headers." occurs. > > What trigger then the error then, well, it is derived from a > function like this: > > (This function will output its display data the "OUTPUT VIEW".) > > function ug_show_files($Dir, $pattern, $Message) > { > print "<pre>$Message\r\n"; > > $ListDir = "dir $Dir\\*.$pattern"; > if ($pipe = popen($ListDir, "r")) { > > while (!feof($pipe)) { > $line = trim(fgets($pipe)); > print "$line\n"; > } > > pclose($pipe); > } > print"<pre>"; > return "ok"; > } > > The above code list a given directory based on $pattern. > However whenever no files are found, then the above CGI error > is generated - but never if there is a content found. I was a > bit confused about this, since the only difference is that the > directory listing does not generate an output, so in my point > of view it should work, but it did not. > > > I boiled down the problem to be related with fgets() - the error > occurs when the directory listing does not return anything, so I > thought I would be able to get rid of the error by writing the code > like this instead: > > function ug_show_files($Dir, $pattern, $Message) > { > print "<pre>$Message\r\n"; > > $ListDir = "dir $Dir\\*.$pattern"; > if ($pipe = popen($ListDir, "r")) { > > $a = fgets($pipe) > while (!feof($pipe)) { > $line = trim($a); > print "$line\n"; > $a = fgets($pipe); > } > pclose($pipe); > } > print"<pre>"; > return "ok"; > } > > > However the error persisted whenever the directory list > was empty so I changed the code to look like this: > > function ug_show_files($Dir, $pattern, $Message) > { > print "<pre>$Message\r\n"; > > $ListDir = "dir $Dir"; > if ($pipe = popen($ListDir, "r")) { > > while (!feof($pipe)) { > > $line = trim(fgets($pipe)); > if (strpos($line, ".$pattern")) { > print "$line\n"; > } > > } > pclose($pipe); > } > print"<pre>"; > return "ok"; > } > > And after this the pages stop returning an error. Now to the > question; why is it that fgets() generates this CGI error > when fgets() read from an empty pipe? -- PHP Windows Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php