PHP 7.2.7 Released

Flux d'entrée/sortie

Le CLI SAPI définit quelques constantes pour les flux I/O pour rendre la programmation en ligne de commande plus facile.

Constantes spécifiques CLI
Constante Description

Un flux déjà ouvert vers stdin. Ceci évite de l'ouvrir explicitement avec

Si vous voulez lire une seule ligne depuis stdin, vous pouvez utiliser
trim(fgets(STDIN)); // lit une ligne depuis STDIN
fscanf(STDIN"%d\n"$number); // lit des nombres depuis STDIN


Un flux déjà ouvert vers stdout. Ceci évite de l'ouvrir explicitement avec



Un flux déjà ouvert vers stderr. Ceci évite de l'ouvrir explicitement avec


Ainsi, vous n'avez pas besoin d'ouvrir un flux spécifique pour, par exemple, stderr mais vous pouvez simplement utiliser la constante correspondante à ce flux :

php -r 'fwrite(STDERR, "stderr\n");'
Vous n'avez pas à clore explicitement ces flux, sachant qu'ils le seront automatiquement par PHP à la fin de votre script.


Ces constantes ne sont pas disponibles lors d'une lecture d'un script PHP depuis stdin.

add a note add a note

User Contributed Notes 4 notes

Aurelien Marchand
7 years ago
Please remember in multi-process applications (which are best suited under CLI), that I/O operations often will BLOCK signals from being processed.

For instance, if you have a parent waiting on fread(STDIN), it won't handle SIGCHLD, even if you defined a signal handler for it, until after the call to fread has returned.

Your solution in this case is to wait on stream_select() to find out whether reading will block. Waiting on stream_select(), critically, does NOT BLOCK signals from being processed.

ecrist at secure-computing dot net
6 years ago
The following code shows how to test for input on STDIN.  In this case, we were looking for CSV data, so we use fgetcsv to read STDIN, if it creates an array, we assume CVS input on STDIN, if no array was created, we assume there's no input from STDIN, and look, later, to an argument with a CSV file name.

Note, without the stream_set_blocking() call, fgetcsv() hangs on STDIN, awaiting input from the user, which isn't useful as we're looking for a piped file. If it isn't here already, it isn't going to be.

(STDIN, 0);
$csv_ar = fgetcsv(STDIN);
if (
"CVS on STDIN\n";
} else {
"Look to ARGV for CSV file name.\n";
ayon at hyurl dot com
1 year ago
I find a BUG with the constant STDIN, I don't know if it si the Enter/Return key that make this proprem, when I use trim(fgets(STDIN)), that doesn't trim anything, when I detect the length of fgets(STDIN), in windows, it is 2 characters longer than what I input, in Linux, it makes 1. I tried to trim(fgets(STDIN), ' \r\n'), but it still does not work.
So I have to substr the input manually, it seems like this way:
= trim(substr(fgets(STDIN), 0, (PHP_OS == 'WINNT' ? 2 : 1)));
then I get what I want really.
James Zhu
7 years ago

function ReadStdin($prompt, $valid_inputs, $default = '') {
$input) || (is_array($valid_inputs) && !in_array($input, $valid_inputs)) || ($valid_inputs == 'is_file' && !is_file($input))) {
$input = strtolower(trim(fgets(STDIN)));
$input) && !empty($default)) {
$input = $default;

// you can input <Enter> or 1, 2, 3
$choice = ReadStdin('Please choose your answer or press Enter to continue: ', array('', '1', '2', '3'));

// check input is valid file name, use /var/path for input nothing
$file = ReadStdin('Please input the file name(/var/path):', 'is_file', '/var/path');

you can add more functions if you want.
To Top