I am running into a project that read a stream from a txt file, so in the CLI, i write:
cat texte.txt|php index.php
In my index.php
file i read the stream:
$handle = fopen ("php://stdin","r");
Now i have a $result
that contains the result of my processing file and i want to output it with STDOUT
, I looked in the manual, but I don't know how to use it, can you please make me a use example.
php://stdin, php://stdout and php://stderr allow direct access to the corresponding input or output stream of the PHP process. The stream references a duplicate file descriptor, so if you open php://stdin and later close it, you close only your copy of the descriptor-the actual stream referenced by STDIN is unaffected.
php://stdin, php://stdout and php://stderr allow direct access to standard input stream device, standard output stream and error stream to a PHP process respectively. Predefined constants STDIN, STDOUT and STDERR respectively represent these streams.
Stream stdout is opened with fopen("stdout", "w") . Stream stderr is opened with fopen("stderr", "w") .
php://output ¶ php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print and echo.
Okay, let me give you another example for the STDIN
and STDOUT
usage.
In PHP you use these two idioms:
$input = fgets(STDIN); fwrite(STDOUT, $output);
When from the commandline you utilize them as such:
cat "input.txt" | php script.php > "output.txt" php script.php < input.txt > output.txt echo "input..." | php script.php | sort | tee output.txt
That's all these things do. Piping in, or piping out. And the incoming parts will appear in STDIN
, whereas your output should go to STDOUT
. Never cross the streams, folks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With