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?

2
  • 3
    It's standard input; THAT is the stream, not a file. Commented Jan 7, 2012 at 22:22
  • Maybe you don't understand what standard input is: en.wikipedia.org/wiki/… Commented Jan 7, 2012 at 22:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What should i do if i want to read a txt file then, please, i want an example showing the use of that.
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: en.wikipedia.org/wiki/Standard_streams

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.