Re: [PHP] output buffering in CLI script.
Greg Donald schreef: On 2/28/08, Casey <[EMAIL PROTECTED]> wrote: #!/usr/bin/php Or the entirely more portable version: #!/usr/bin/env php thanks for the tip :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
On 2/28/08, Casey <[EMAIL PROTECTED]> wrote: > #!/usr/bin/php Or the entirely more portable version: #!/usr/bin/env php http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
Casey wrote: On Thu, Feb 28, 2008 at 3:39 AM, Jochem Maas <[EMAIL PROTECTED]> wrote: hi there, I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. I use Windows, so I don't need the shebang line. But if I'm correct and the shebang line is: #!/usr/bin/php , shouldn't it be considered a comment, because of the "#"? nope, because it comes before the http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
On Thu, Feb 28, 2008 at 3:39 AM, Jochem Maas <[EMAIL PROTECTED]> wrote: > hi there, > > I can't seem to manage to buffer output (of an included file) in a CLI > script, > the following does not work: > > > // buffer output so we can avoid the shebang line being > output (and strip it from the output we log) > $oldIFvalue = ini_set('implicit_flush', false); > ob_start(); > > if ([EMAIL PROTECTED] $script) { > ob_end_clean(); > } else { > $output = explode("\n", ob_get_clean()); > if ($output[0] && preg_match('%^#!\/%', $output[0])) > unset($output[0]); > } > > ini_set('implicit_flush', $oldIFvalue); > > > the reason I'm wanting to do this is, primarily, in order to stop the > shebang line that *may* > be present in the included script from being output to stdout. > I use Windows, so I don't need the shebang line. But if I'm correct and the shebang line is: #!/usr/bin/php , shouldn't it be considered a comment, because of the "#"? -- -Casey -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: [SOLVEDish] Re: [PHP] output buffering in CLI script.
Nathan Rixham schreef: how's this? #!/usr/bin/php data = ltrim($bucket->data,"#!/usr/bin/php\n"); $consumed += $bucket->datalen; stream_bucket_append($out, $bucket); } return PSFS_PASS_ON; } } stream_filter_register("trim.shebang", "trimshebang"); include "php://filter/read=trim.shebang/resource=/path/to/include.php"; ?> bit more graceful ;) it's sick, I like it, nice example of streams filtering while your at it. I learned something! it also works in so far that the shebang line is no longer output but the output buffering in *my* script(s) doesn't work, whilst the same construction does work in Stut's test scripts (although whilst playing with his script I did manage to bork ik so that the output buffering *doesn't* work there anymore ... how I did that I can't fathom) anyway - they problem has been nicely shoved under the carpet. apart from the shebang lines the script don't ever output anything anyway but rather log to files ... a simple redirect output to /dev/null in the command line that is used to run the 'master' script will take care of any spurious cruft ending up on screen. thanks to everyone for their input regarding my output! :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] [NOT SOLVEDish] Re: [PHP] output buffering in CLI script.
Jochem Maas schreef: ... indeed ... I tested it and it works on my server too. my code is no different to yours with regard to the context of the problem. so what's going on. I think (I know) I forgot to mention one tiny little detail. the whole 'include' code occurs in a script that forks itself ... the script forks itself a number of times and each child then performs the include. so I tested the fork issue with your script ... it still bloody works, (see the for loop below) so it's something else ... I thought it might be the difference between using ob_get_clean() and ob_get_contents()+ob_end_clean() but that doesn't seem to be it either. the worse thing is my script has just stopped outputting the shebang lines of the included script(s) ... oh well ... the problem is solved, dunno how, dunno why, situations like these I'd rather the problem didn't go away .. I prefer to know wtf happened! wtf happened was I was looking at the wrong output. I changed my code to be as much like your test code as possible (to the point that I now use a function to perform the buffering and 'include' and returning output) ... the output buffering is not working at all. I've had enough - /dev/null meet script output, output meet /dev/null #!/usr/bin/php for ($i = 0; $i < 3; $i++) { $pid = pcntl_fork(); if ($pid == 0) { echo Inc('testinc.php'); exit; } } function & Inc($f) { ob_start(); @include($f); $content = ob_get_contents(); ob_end_clean(); if (substr($content, 0, 2) == '#!') { $content = substr($content, strpos($content, "\n")+1); } return $content; } #!/usr/bin/php [EMAIL PROTECTED]:~$ ./test.php arse testinc -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: [SOLVEDish] Re: [PHP] output buffering in CLI script.
Aschwin Wesselius wrote: Me thinks that when shebang doesn't fit /usr/bin/php (which happens sometimes) you're doomed. [snip] good point.. modified to use BINDIR: #!/usr/bin/php data = ltrim($bucket->data,"#!".PHP_BINDIR."/php\n"); $consumed += $bucket->datalen; stream_bucket_append($out, $bucket); } return PSFS_PASS_ON; } } stream_filter_register("trim.shebang", "trimshebang"); include "php://filter/read=trim.shebang/resource=/path/to/include.php"; ?> But it still is a nice example. thanks :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: [SOLVEDish] Re: [PHP] output buffering in CLI script.
Nathan Rixham wrote: how's this? #!/usr/bin/php data = ltrim($bucket->data,"#!/usr/bin/php\n"); $consumed += $bucket->datalen; stream_bucket_append($out, $bucket); } return PSFS_PASS_ON; } } stream_filter_register("trim.shebang", "trimshebang"); include "php://filter/read=trim.shebang/resource=/path/to/include.php"; ?> bit more graceful ;) Me thinks that when shebang doesn't fit /usr/bin/php (which happens sometimes) you're doomed. But it still is a nice example. -- Aschwin Wesselius What you would like to be done to you, do that to the other -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: [SOLVEDish] Re: [PHP] output buffering in CLI script.
how's this? #!/usr/bin/php data = ltrim($bucket->data,"#!/usr/bin/php\n"); $consumed += $bucket->datalen; stream_bucket_append($out, $bucket); } return PSFS_PASS_ON; } } stream_filter_register("trim.shebang", "trimshebang"); include "php://filter/read=trim.shebang/resource=/path/to/include.php"; ?> bit more graceful ;) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] [SOLVEDish] Re: [PHP] output buffering in CLI script.
Stuart Dallas schreef: On 28 Feb 2008, at 12:29, Jochem Maas wrote: Stuart Dallas schreef: On 28 Feb 2008, at 11:52, Stut wrote: On 28 Feb 2008, at 11:39, Jochem Maas wrote: I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. This works... indeed ... I tested it and it works on my server too. my code is no different to yours with regard to the context of the problem. so what's going on. I think (I know) I forgot to mention one tiny little detail. the whole 'include' code occurs in a script that forks itself ... the script forks itself a number of times and each child then performs the include. so I tested the fork issue with your script ... it still bloody works, (see the for loop below) so it's something else ... I thought it might be the difference between using ob_get_clean() and ob_get_contents()+ob_end_clean() but that doesn't seem to be it either. the worse thing is my script has just stopped outputting the shebang lines of the included script(s) ... oh well ... the problem is solved, dunno how, dunno why, situations like these I'd rather the problem didn't go away .. I prefer to know wtf happened! #!/usr/bin/php for ($i = 0; $i < 3; $i++) { $pid = pcntl_fork(); if ($pid == 0) { echo Inc('testinc.php'); exit; } } function & Inc($f) { ob_start(); @include($f); $content = ob_get_contents(); ob_end_clean(); if (substr($content, 0, 2) == '#!') { $content = substr($content, strpos($content, "\n")+1); } return $content; } #!/usr/bin/php [EMAIL PROTECTED]:~$ ./test.php arse testinc -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
Nathan Rixham schreef: Jochem Maas wrote: ... -Stut bit of false logic here but have you tried: eval(ltrim(file_get_contents($script),$shebang)); haven't tried it, did consider it. I hate eval() :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
Jochem Maas wrote: I mean that the shebang line at the top of the included file is output to stdout even when I turn on output buffering on prior to including the file. nothing else is output but that's because the script doesn't generate any further output - it logs everything instead - and I know it runs because the relevant lines appear in the relevant log file. I don't know what goes wrong here. I took your script and it just works, so it is not your code (I guess). I've done this: $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if (!include 'scrap2.php') { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!%', $output[0])) unset($output[0]); echo implode("\n", $output); } ini_set('implicit_flush', $oldIFvalue); The only difference is it echoes imploded $output. And I do a bit different preg_match ^#! instead of ^#!\/ If you can include the script you want to include, than it should work. If you can buffer output without the include file, than it should work. If you can dump $output after you've done the explode, than it should work. So, I don't know what goes wrong. -- Aschwin Wesselius What you would like to be done to you, do that to the other -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
On 28 Feb 2008, at 12:29, Jochem Maas wrote: Stuart Dallas schreef: On 28 Feb 2008, at 11:52, Stut wrote: On 28 Feb 2008, at 11:39, Jochem Maas wrote: I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. This works... #!/usr/bin/php #!/usr/bin/php [EMAIL PROTECTED]:~$ ./test.php arse testinc -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
Jochem Maas wrote: Stut schreef: On 28 Feb 2008, at 11:39, Jochem Maas wrote: I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. On my local machine here PHP does not output the shebang line. AFAIK it detects and strips it at the compilation phase. Have you tried it? I have, I actually wrote the code assuming that the shebang line is ignored (as I had experienced and read about ages ago), I was surprised to see the shebang line output in the shell ... so I figured I'd just buffer and strip the line manually given that php decided to treat the shebang line as regular output when it's the first line of an *included* file ... but no joy on getting output buffering to work in the shell, STFW didn't do it for me either, so now I'm hassling you guys :-) obviously the shebang line doesn't get output if I run said script directly (or by using exec) ... and yes I need to be able to do it both ways :-) -Stut bit of false logic here but have you tried: eval(ltrim(file_get_contents($script),$shebang)); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
Stuart Dallas schreef: On 28 Feb 2008, at 11:52, Stut wrote: On 28 Feb 2008, at 11:39, Jochem Maas wrote: I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. On my local machine here PHP does not output the shebang line. AFAIK it detects and strips it at the compilation phase. Have you tried it? Hang on, ignore that. You're including the file, not running it on the CLI. What do you mean when you say it doesn't work? Do you mean you get no output? I mean that the shebang line at the top of the included file is output to stdout even when I turn on output buffering on prior to including the file. nothing else is output but that's because the script doesn't generate any further output - it logs everything instead - and I know it runs because the relevant lines appear in the relevant log file. I suggest you remove the @ at the start of the include while developing it. Or alternatively spit something out after you call ob_end_clean so you know it couldn't include the file. the @ is niether here nor there with regard to the problem I have, well more of an annoyance - nobody is going get hurt by some spurious output to stdout but it looks messy. just to clarify - I need to be able to run the script directly (as an executable) and be able to include it within another script ... so the shebang line is needed (well I could hack around it with a wrapper script to use directly on the command line or run it using /usr/bin/php foo.php ... but I don't find either of those satisfactory) -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
Stut schreef: On 28 Feb 2008, at 11:39, Jochem Maas wrote: I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. On my local machine here PHP does not output the shebang line. AFAIK it detects and strips it at the compilation phase. Have you tried it? I have, I actually wrote the code assuming that the shebang line is ignored (as I had experienced and read about ages ago), I was surprised to see the shebang line output in the shell ... so I figured I'd just buffer and strip the line manually given that php decided to treat the shebang line as regular output when it's the first line of an *included* file ... but no joy on getting output buffering to work in the shell, STFW didn't do it for me either, so now I'm hassling you guys :-) obviously the shebang line doesn't get output if I run said script directly (or by using exec) ... and yes I need to be able to do it both ways :-) -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
Andrés Robinet schreef: -Original Message- From: Jochem Maas [mailto:[EMAIL PROTECTED] Sent: Thursday, February 28, 2008 6:39 AM To: [php] PHP General List Subject: [PHP] output buffering in CLI script. hi there, I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. -- Aren't you deleting the output when you do ob_get_clean? Meaning, you are missing an echo of the captured buffer, like: $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); echo join("\n", $output); // You need to echo this, since you lost your buffer by doing ob_get_clean I'm retrieving the output buffer and shoving it in $output - which is subsequently logged to a file. but output is not being buffered at all regardless of implicit_flush setting and/or use of ob_start() :-( Regards, Rob Andrés Robinet | Lead Developer | BESTPLACE CORPORATION 5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308 | TEL 954-607-4207 | FAX 954-337-2695 | Email: [EMAIL PROTECTED] | MSN Chat: [EMAIL PROTECTED] | SKYPE: bestplace | Web: bestplace.biz | Web: seo-diy.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
On 28 Feb 2008, at 11:52, Stut wrote: On 28 Feb 2008, at 11:39, Jochem Maas wrote: I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. On my local machine here PHP does not output the shebang line. AFAIK it detects and strips it at the compilation phase. Have you tried it? Hang on, ignore that. You're including the file, not running it on the CLI. What do you mean when you say it doesn't work? Do you mean you get no output? I suggest you remove the @ at the start of the include while developing it. Or alternatively spit something out after you call ob_end_clean so you know it couldn't include the file. -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] output buffering in CLI script.
On 28 Feb 2008, at 11:39, Jochem Maas wrote: I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. On my local machine here PHP does not output the shebang line. AFAIK it detects and strips it at the compilation phase. Have you tried it? -Stut -- http://stut.net/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] output buffering in CLI script.
> -Original Message- > From: Jochem Maas [mailto:[EMAIL PROTECTED] > Sent: Thursday, February 28, 2008 6:39 AM > To: [php] PHP General List > Subject: [PHP] output buffering in CLI script. > > hi there, > > I can't seem to manage to buffer output (of an included file) in a CLI > script, > the following does not work: > > > // buffer output so we can avoid the shebang line > being output (and strip it from the output we log) > $oldIFvalue = ini_set('implicit_flush', false); > ob_start(); > > if ([EMAIL PROTECTED] $script) { > ob_end_clean(); > } else { > $output = explode("\n", ob_get_clean()); > if ($output[0] && preg_match('%^#!\/%', > $output[0])) > unset($output[0]); > } > > ini_set('implicit_flush', $oldIFvalue); > > > the reason I'm wanting to do this is, primarily, in order to stop the > shebang line that *may* > be present in the included script from being output to stdout. > > -- Aren't you deleting the output when you do ob_get_clean? Meaning, you are missing an echo of the captured buffer, like: $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); echo join("\n", $output); // You need to echo this, since you lost your buffer by doing ob_get_clean Regards, Rob Andrés Robinet | Lead Developer | BESTPLACE CORPORATION 5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL 33308 | TEL 954-607-4207 | FAX 954-337-2695 | Email: [EMAIL PROTECTED] | MSN Chat: [EMAIL PROTECTED] | SKYPE: bestplace | Web: bestplace.biz | Web: seo-diy.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] output buffering in CLI script.
hi there, I can't seem to manage to buffer output (of an included file) in a CLI script, the following does not work: // buffer output so we can avoid the shebang line being output (and strip it from the output we log) $oldIFvalue = ini_set('implicit_flush', false); ob_start(); if ([EMAIL PROTECTED] $script) { ob_end_clean(); } else { $output = explode("\n", ob_get_clean()); if ($output[0] && preg_match('%^#!\/%', $output[0])) unset($output[0]); } ini_set('implicit_flush', $oldIFvalue); the reason I'm wanting to do this is, primarily, in order to stop the shebang line that *may* be present in the included script from being output to stdout. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php