note 125106 added to features.commandline.io-streams
| From: | phil_php at zieaon dot com | Date: | Wed, 10 Jun 2020 04:42:08 +0000 |
| Subject: | note 125106 added to features.commandline.io-streams | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214753@lists.php.net to get a copy of this message | ||
Under Linux CLI - STDIN, STDOUT and STDERR can be closed and reconnected to a different php stream
such as a file, pipe or even a UDP socket_stream. (I use this technique to send the output/errors of
my long running background scripts to a file so I can debug if something goes wrong.)
For example: (The below creates/appends file "/tmp/php_stdout.txt")
<?php
// This only works under CLI in Linux
// Note: Until we have closed it STDOUT will NOT be prefixed with a $
// Get the path to the current console for STDOUT so we can reconnect later!
$strOldSTDOUT=(posix_ttyname(STDOUT));
echo("This will go to the current console\r\n");
// Close the STDOUT resource
fclose(STDOUT);
// Reopen $STDOUT as a file Note: All further $STDOUT usage will be prefixed with a $
$STDOUT=fopen("/tmp/php_stdout.txt","a"); /
echo("This should append the file /tmp/php_stdout.txt\r\n");
// Close stdout again so we can reconnect the console. Note: We are still using
fclose($STDOUT);
// Use the path to the console we got earlier
$STDOUT=fopen($strOldSTDOUT,"r+");
echo("And we are back on the console\r\n");
?>
----
Server IP: 45.112.84.4
Probable Submitter: 101.0.109.162 (proxied: 203.192.75.125)
----
Manual Page -- http://php.net/manual/en/features.commandline.io-streams.php
Edit -- https://master.php.net/note/edit/125106
Del: integrated -- https://master.php.net/note/delete/125106/integrated
Del: useless -- https://master.php.net/note/delete/125106/useless
Del: bad code -- https://master.php.net/note/delete/125106/bad+code
Del: spam -- https://master.php.net/note/delete/125106/spam
Del: non-english -- https://master.php.net/note/delete/125106/non-english
Del: in docs -- https://master.php.net/note/delete/125106/in+docs
Del: other reasons-- https://master.php.net/note/delete/125106
Reject -- https://master.php.net/note/reject/125106
Search -- https://master.php.net/manage/user-notes.php