Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

23. Files > 23.4. Reading from Standard Input

23.4. Reading from Standard Input

Problem

You want to read from standard input in a command-line context—for example, to get user input from the keyboard or data piped to your PHP program.

Solution

Use fopen() to open php://stdin, as in Example 23-15.

Example 23-15. Reading from standard input
<?php
$fh = fopen('php://stdin','r') or die($php_errormsg);
while($s = fgets($fh)) {
    print "You typed: $s";
}
?>

Discussion

Recipe 25.3 discusses reading data from the keyboard in a command-line context in more detail. Reading data from standard input isn’t very useful in a web context, because information doesn’t arrive via standard input. The bodies of HTTP post and file-upload requests are parsed by PHP and put into special variables. Non-file-upload post request bodies can also be read with the php://input stream, as discussed in Recipe 8.7.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint