0

I know the way to open and read the content of the file with the fopen function like this:

@fopen("inputfile.txt", "r");

But with the php://stdin i got bit confused

$in = fopen('php://stdin', 'r');

Where should i specify the name of txt file that i attempt to read?

Malloc
  • 15,434
  • 34
  • 105
  • 192

1 Answers1

4

php://stdin is for CLI usage. You do not specify a filename directly in PHP then. It is typically utilized from the terminal like this:

echo "input text" | php script.php

or

cat textinput.txt | php do-something.php

Where the thing you pipe into the interpreter is what you receive when reading from stdin.

Btw, you can also just use the STDIN constant, without manually calling fopen first.

mario
  • 144,265
  • 20
  • 237
  • 291
  • What should i do if i want to read a `txt` file then, please, i want an example showing the use of that. – Malloc Jan 07 '12 at 22:28
  • For reading a file, see my second example. You do not specify the file in your PHP script, but leave that to users and the shell. That's the whole point of STDIN, to allow for input from unknown sources. -- I guess Wikipedia for once explains the concept better than I ever could: http://en.wikipedia.org/wiki/Standard_streams – mario Jan 07 '12 at 22:29