constants STDOUT, STDERR, STDIN not working in 5.2.x?

constants STDOUT, STDERR, STDIN not working in 5.2.x?

am 23.03.2010 11:47:41 von Marten Lehmann

Hello,

I found different code examples like this, which use the file handle
STDERR just like this:

fwrite(STDERR, "hello\n");
?>

Also, the PHP documentation of input/output streams
(http://php.net/manual/de/wrappers.php.php) says:

"It is recommended that you simply use the constants STDIN, STDOUT and
STDERR instead of manually opening streams using these wrappers."

I don't want to use the "php://stderr" wrapper, because this just
creates a duplicate of the original STDERR handler and if I'm closing
"php://stderr", the original STDERR still would exist.

When I'm using this code, I only get:

Notice: Use of undefined constant STDERR - assumed 'STDERR' in
/test.php on line 4



Warning: fwrite(): supplied argument is not a valid stream
resource in /test.php on line 4


How can I access the original STDERR handle? The constant should be
there, but does not exist.

regards
Marten

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: constants STDOUT, STDERR, STDIN not working in 5.2.x?

am 23.03.2010 12:09:45 von ro0ot.w00t

--0016e6dd9820ea1b23048275d95a
Content-Type: text/plain; charset=ISO-8859-1

2010/3/23 Marten Lehmann

> Hello,
>
> I found different code examples like this, which use the file handle STDERR
> just like this:
>
> > fwrite(STDERR, "hello\n");
> ?>
>
> Also, the PHP documentation of input/output streams (
> http://php.net/manual/de/wrappers.php.php) says:
>
> "It is recommended that you simply use the constants STDIN, STDOUT and
> STDERR instead of manually opening streams using these wrappers."
>
> I don't want to use the "php://stderr" wrapper, because this just creates a
> duplicate of the original STDERR handler and if I'm closing "php://stderr",
> the original STDERR still would exist.
>
> When I'm using this code, I only get:
>
> Notice: Use of undefined constant STDERR - assumed 'STDERR' in
> /test.php on line 4

>

> Warning: fwrite(): supplied argument is not a valid stream resource
> in /test.php on line 4

>
> How can I access the original STDERR handle? The constant should be there,
> but does not exist.
>
> regards
> Marten
>
>
Hi,

I can reproduce it with some differences. Check this out:

$ php --version
PHP 5.2.10-2ubuntu6.4 with Suhosin-Patch 0.9.7 (cli) (built: Jan 6 2010
22:56:44)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies

$ php 1>/dev/null # note that the PHP error messages are
displayed on STDOUT - I don't show them with "1>/dev/null"
$x = fopen('php://stderr', 'r'); fwrite($x, "test\n"); fclose($x);
$x = fopen(STDERR, 'r'); fwrite($x, "test2\n"); fclose($x); ?>
------------
fwrite(STDERR, "test3\n");
fwrite ('php://stderr', "test4");
?>
-----------

Result:

test


--------

So my assumption is, that the quoted recommendation is outdated / wrong.

Regards


PS: PHP reports this when giving the constant STDERR:
Warning: fopen(STDERR): failed to open stream: No such file or directory
Warning: fwrite(): supplied argument is not a valid stream resource

--0016e6dd9820ea1b23048275d95a--

Re: constants STDOUT, STDERR, STDIN not working in 5.2.x?

am 23.03.2010 15:26:23 von Daniel Egeberg

On Tue, Mar 23, 2010 at 11:47, Marten Lehmann wrote:
> Hello,
>
> I found different code examples like this, which use the file handle STDE=
RR
> just like this:
>
> > fwrite(STDERR, "hello\n");
> ?>
>
> Also, the PHP documentation of input/output streams
> (http://php.net/manual/de/wrappers.php.php) says:
>
> "It is recommended that you simply use the constants STDIN, STDOUT  =
and
> STDERR instead of manually opening streams using these wrappers."
>
> I don't want to use the "php://stderr" wrapper, because this just creates=
a
> duplicate of the original STDERR handler and if I'm closing "php://stderr=
",
> the original STDERR still would exist.
>
> When I'm using this code, I only get:
>
> Notice:  Use of undefined constant STDERR - assumed 'STDERR' =
in
> /test.php on line 4

>

> Warning:  fwrite(): supplied argument is not a valid stream r=
esource
> in /test.php on line 4

>
> How can I access the original STDERR handle? The constant should be there=
,
> but does not exist.
>
> regards
> Marten

These I/O streams are only present in the CLI SAPI.

--=20
Daniel Egeberg

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: constants STDOUT, STDERR, STDIN not working in 5.2.x?

am 23.03.2010 15:50:06 von ro0ot.w00t

--00032555b7aefd781c048278eda3
Content-Type: text/plain; charset=ISO-8859-1

2010/3/23 Daniel Egeberg

> On Tue, Mar 23, 2010 at 11:47, Marten Lehmann wrote:
> > Hello,
> >
> > I found different code examples like this, which use the file handle
> STDERR
> > just like this:
> >
> > > > fwrite(STDERR, "hello\n");
> > ?>
> >
> > Also, the PHP documentation of input/output streams
> > (http://php.net/manual/de/wrappers.php.php) says:
> >
> > "It is recommended that you simply use the constants STDIN, STDOUT and
> > STDERR instead of manually opening streams using these wrappers."
> >
> > I don't want to use the "php://stderr" wrapper, because this just creates
> a
> > duplicate of the original STDERR handler and if I'm closing
> "php://stderr",
> > the original STDERR still would exist.
> >
> > When I'm using this code, I only get:
> >
> > Notice: Use of undefined constant STDERR - assumed 'STDERR' in
> > /test.php on line 4

> >

> > Warning: fwrite(): supplied argument is not a valid stream
> resource
> > in /test.php on line 4

> >
> > How can I access the original STDERR handle? The constant should be
> there,
> > but does not exist.
> >
> > regards
> > Marten
>
> These I/O streams are only present in the CLI SAPI.
>
> --
> Daniel Egeberg
>
> Please confirm that the code of my previous replay on this topic (executed
via php-cli) does not print out "test2" or "test3". The constant is present
but doesn't work as supposed.

Regards

--00032555b7aefd781c048278eda3--

Re: constants STDOUT, STDERR, STDIN not working in 5.2.x?

am 23.03.2010 16:39:12 von Daniel Egeberg

On Tue, Mar 23, 2010 at 15:50, Jan G.B. wrote:
>
>
> 2010/3/23 Daniel Egeberg
>>
>> On Tue, Mar 23, 2010 at 11:47, Marten Lehmann wrote:
>> > Hello,
>> >
>> > I found different code examples like this, which use the file handle
>> > STDERR
>> > just like this:
>> >
>> > >> > fwrite(STDERR, "hello\n");
>> > ?>
>> >
>> > Also, the PHP documentation of input/output streams
>> > (http://php.net/manual/de/wrappers.php.php) says:
>> >
>> > "It is recommended that you simply use the constants STDIN, STDOUT =C2=
=A0and
>> > STDERR instead of manually opening streams using these wrappers."
>> >
>> > I don't want to use the "php://stderr" wrapper, because this just
>> > creates a
>> > duplicate of the original STDERR handler and if I'm closing
>> > "php://stderr",
>> > the original STDERR still would exist.
>> >
>> > When I'm using this code, I only get:
>> >
>> > Notice:  Use of undefined constant STDERR - assumed 'STDER=
R' in
>> > /test.php on line 4

>> >

>> > Warning:  fwrite(): supplied argument is not a valid strea=
m
>> > resource
>> > in /test.php on line 4

>> >
>> > How can I access the original STDERR handle? The constant should be
>> > there,
>> > but does not exist.
>> >
>> > regards
>> > Marten
>>
>> These I/O streams are only present in the CLI SAPI.
>>
>> --
>> Daniel Egeberg
>>
> Please confirm that the code of my previous replay on this topic (execute=
d
> via php-cli) does not print out "test2" or "test3". The constant is prese=
nt
> but doesn't work as supposed.
> Regards
>
>

You are making a number of errors there:
1) You are trying to open php://stderr in read mode, but that stream
is write only (I'm not sure why it outputs regardless).
2) You are trying to open STDERR, but that constant holds a stream
resource while fopen() expects a string.
3) You are trying to write to 'php://stderr'. That's not possible. You
cannot pass a string as stream.

The following should work:

daniel@daniel-laptop:~$ cat test.php
$stderr =3D fopen('php://stderr', 'w');
fwrite($stderr, 'Error 1' . PHP_EOL);
fwrite(STDERR, 'Error 2' . PHP_EOL);
echo 'Normal echo';
daniel@daniel-laptop:~$ php test.php > /dev/null
Error 1
Error 2
daniel@daniel-laptop:~$ ./src/php-5.2.12/sapi/cli/php test.php > /dev/null
Error 1
Error 2

--=20
Daniel Egeberg

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: constants STDOUT, STDERR, STDIN not working in 5.2.x?

am 24.03.2010 14:12:02 von Marten Lehmann

Hello,

> daniel@daniel-laptop:~$ php test.php> /dev/null
> Error 1
> Error 2
> daniel@daniel-laptop:~$ ./src/php-5.2.12/sapi/cli/php test.php> /dev/null
> Error 1
> Error 2

well, using php-cli instead of php-cgi, this finally worked:

fwrite(STDERR, "test\n");
fclose(STDERR);
?>

But why doesn't it work with php-cgi? That's a binary that is called
with the php-file is parameter the same way as php-cli. So both come
with STDIN/STDOUT/STDERR contrary to scripts running through mod_php.

What is the real difference between php-cli and php-cgi? This
"fclose(STDERR);" is really important for one customer, because this is
seems to be the only way to suppress later output to STDERR in the same
script by external commands and functions that are too hard to change.
But I don't want to break things for all other customers just to make
one happier.

Can the STDERR constands be enable somehow at compile-time for php-cgi?

Regards
Marten

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: constants STDOUT, STDERR, STDIN not working in 5.2.x?

am 24.03.2010 14:17:18 von Daniel Egeberg

On Wed, Mar 24, 2010 at 14:12, Marten Lehmann wrote:
> Hello,
>
>> daniel@daniel-laptop:~$ php test.php>  /dev/null
>> Error 1
>> Error 2
>> daniel@daniel-laptop:~$ ./src/php-5.2.12/sapi/cli/php test.php>  /d=
ev/null
>> Error 1
>> Error 2
>
> well, using php-cli instead of php-cgi, this finally worked:
>
> > fwrite(STDERR, "test\n");
> fclose(STDERR);
> ?>
>
> But why doesn't it work with php-cgi? That's a binary that is called with
> the php-file is parameter the same way as php-cli. So both come with
> STDIN/STDOUT/STDERR contrary to scripts running through mod_php.
>
> What is the real difference between php-cli and php-cgi? This
> "fclose(STDERR);" is really important for one customer, because this is
> seems to be the only way to suppress later output to STDERR in the same
> script by external commands and functions that are too hard to change. Bu=
t I
> don't want to break things for all other customers just to make one happi=
er.
>
> Can the STDERR constands be enable somehow at compile-time for php-cgi?
>
> Regards
> Marten

You can see how the CLI SAPI differs from the other SAPIs here:
http://php.net/manual/en/features.commandline.differences.ph p

--=20
Daniel Egeberg

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: constants STDOUT, STDERR, STDIN not working in 5.2.x?

am 24.03.2010 15:43:41 von ro0ot.w00t

--0016e6dd8bb5dac41704828cf485
Content-Type: text/plain; charset=ISO-8859-1

2010/3/24 Marten Lehmann

> Hello,
>
>
> daniel@daniel-laptop:~$ php test.php> /dev/null
>> Error 1
>> Error 2
>> daniel@daniel-laptop:~$ ./src/php-5.2.12/sapi/cli/php test.php>
>> /dev/null
>> Error 1
>> Error 2
>>
>
> well, using php-cli instead of php-cgi, this finally worked:
>
> > fwrite(STDERR, "test\n");
> fclose(STDERR);
> ?>
>
>
Weird, this does not work with the Ubuntu Version I'm using.
As reported in my previous email, I only can print out to STDERR when I
properly use php://stderr with fopen() - no matter if I use read or write
mode in fopen(). So be cautious when developing for different systems. Here
are some examples:

*** WORKS:
$ php

Foo

*** FAILURE:
##############
$ php
fwrite(STDERR, "test");
?>
Warning: fwrite(): supplied argument is not a valid stream resource in
/home/user/- on line 2

###############
$ php
fwrite(STDERR, 'Bar');
fclose($x); ?>
Foo
Warning: fwrite(): supplied argument is not a valid stream resource in
/home/user/- on line 2

###############
$ php

// Nothing happens, no warning, no error and no "Foo"




But why doesn't it work with php-cgi? That's a binary that is called with
> the php-file is parameter the same way as php-cli. So both come with
> STDIN/STDOUT/STDERR contrary to scripts running through mod_php.
>
> What is the real difference between php-cli and php-cgi? This
> "fclose(STDERR);" is really important for one customer, because this is
> seems to be the only way to suppress later output to STDERR in the same
> script by external commands and functions that are too hard to change. But I
> don't want to break things for all other customers just to make one happier.
>

Well, see this..
$ php

string(6) "STDERR"

:-(


> Can the STDERR constands be enable somehow at compile-time for php-cgi?
>
> Regards
> Marten
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

--0016e6dd8bb5dac41704828cf485--