I'm working on some PHP cli tools for a php framework and there's a situation where my script either reads from a file or STDIN
. Since not all operations (like fseek()
) are valid on STDIN
, I'm looking for a way to detect this.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Turns out that the function
stream_get_meta_data()
provides a solution, when called on standard in, the result is:So you can do a simple string compare on the uri:
This solution will work whether the constant stream
STDIO
is used, or the oldfopen('php://stdin', 'r')
, which still lurks around in old code.Simply check
if($fp === STDIN)