Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PassThru... I'm stuck

Status
Not open for further replies.

bandi

Programmer
May 21, 2000
8
HU
I have to run a UNIX program and pass its raw output back to the browser. Is this what the PassThru is for?<br><br>I've tried to use this:<br>&nbsp;&nbsp;&nbsp;&nbsp;<b><FONT FACE=monospace>passthru( 'some.cgi');</font></b><br><br>But I got back the raw output in the browser as text, so the PHP sends the header &quot;Content-type:text/html&quot; before the some.cgi-s raw output. Is it a bug or am I on the wrong track?<br><br>Do you have any idea how to send a cgi-s raw output directly to the browser? (The cgi-s output starts with the proper headers.)<br><br>Thanks in advance,<br>bandi.
 
If you are trying to display the cgi file's code, not the parsed info, then use the file() function to read in the entire file and then spit it out.<br><br>To execute the script, and display the results, you can try the exec() function or even an fopen()/fread() call.<br><br>Either way, try not to use the passthru() because it was not really intended for what you are doing.
 
Keep in mind that passthru() is for working with binary output (in general) which is why the exec() or system() functions are better when executing scripts of this nature.
 
Thanks for the tips, but I needed something else than you thought.<br><br>I wanted to create a php what calls a cgi, and sends back the cgi's output to the browser. The output of the cgi is ready to the browser: first it contains the headers, then the binary data. And it's ok, when you call the cgi directly from the browser.<br>What I've experienced: if I call the cgi with passthru, then the php will send 'Content-type:text/html' to the browser _before_ passing thru the stuff. But, if I set the proper Content-type with header() before passthru, then the php won't send any header, and this is good for me in this case. So after knowing this I have the solution... but not with passthru :)<br><br><FONT FACE=monospace><b><br>$cgi_path = '/cgi-bin/some.cgi';<br>$query = 'something=value';<br><br>$fp = fsockopen( &quot;localhost&quot;, 80, &$errno, &$errstr);<br><br>if( !$fp) {<br>&nbsp;&nbsp;&nbsp;&nbsp;die( &quot;$errstr ($errno)&quot;);<br>} else {<br>&nbsp;&nbsp;&nbsp;&nbsp;fputs( $fp, &quot;GET $cgi_path?$query HTTP/1.0\n\n&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;</b>// first reading the headers from the cgi's output and sendig to the browser as header<b><br>&nbsp;&nbsp;&nbsp;&nbsp;while( !feof( $fp)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$tmp = fgets( $fp, 128);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header( chop($tmp));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( eregi( &quot;Content-Type&quot;, $tmp)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$tmp = fgets( $fp, 128);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;</b>// then passing thru the whole binary output<b><br>&nbsp;&nbsp;&nbsp;&nbsp;fpassthru( $fp);<br>&nbsp;&nbsp;&nbsp;&nbsp;fclose($fp);<br>}<br></b></font><br><br>I hope it'll be useful for some readers,<br><br>bandi.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top