我有一个脚本,可以作为网页运行,也可以通过控制台运行.
检测用于调用脚本的方法看起来非常简单,但是当从控制台运行脚本时,我需要知道脚本是否以交互方式运行(用户键入命令,或从文件重定向输入) .
php script.php
与
php script.php < input_file
这可能吗?
Lei*_*igh 33
我还需要一个比posix_isatty检测到的更灵活的解决方案:
经过一些实验和挖掘libc标题后,我想出了一个非常简单的类,可以完成上述所有操作.
class IOMode
{
public $stdin;
public $stdout;
public $stderr;
private function getMode(&$dev, $fp)
{
$stat = fstat($fp);
$mode = $stat['mode'] & 0170000; // S_IFMT
$dev = new StdClass;
$dev->isFifo = $mode == 0010000; // S_IFIFO
$dev->isChr = $mode == 0020000; // S_IFCHR
$dev->isDir = $mode == 0040000; // S_IFDIR
$dev->isBlk = $mode == 0060000; // S_IFBLK
$dev->isReg = $mode == 0100000; // S_IFREG
$dev->isLnk = $mode == 0120000; // S_IFLNK
$dev->isSock = $mode == 0140000; // S_IFSOCK
}
public function __construct()
{
$this->getMode($this->stdin, STDIN);
$this->getMode($this->stdout, STDOUT);
$this->getMode($this->stderr, STDERR);
}
}
$io = new IOMode;
Run Code Online (Sandbox Code Playgroud)
一些示例用法,以显示它可以检测到的内容.
输入:
$ php io.php
// Character device as input
// $io->stdin->isChr == true
$ echo | php io.php
// Input piped from another command
// $io->stdin->isFifo == true
$ php io.php < infile
// Input from a regular file (name taken verbatim from C headers)
// $io->stdin->isReg == true
$ mkdir test
$ php io.php < test
// Directory used as input
// $io->stdin->isDir == true
Run Code Online (Sandbox Code Playgroud)
输出:
$ php io.php
// $io->stdout->isChr == true
$ php io.php | cat
// $io->stdout->isFifo == true
$ php io.php > outfile
// $io->stdout->isReg == true
Run Code Online (Sandbox Code Playgroud)
错误:
$ php io.php
// $io->stderr->isChr == true
$ php io.php 2>&1 | cat
// stderr redirected to stdout AND piped to another command
// $io->stderr->isFifo == true
$ php io.php 2>error
// $io->stderr->isReg == true
Run Code Online (Sandbox Code Playgroud)
我没有包含链接,套接字或块设备的示例,但没有理由它们不应该工作,因为它们的设备模式掩码在类中.
(未在Windows上测试 - 里程可能会有所不同)
if (posix_isatty(0)) {
// STDIN is a TTY
} else {
// STDIN is a pipe or has no associated TTY
}
Run Code Online (Sandbox Code Playgroud)
显然,这仅适用于POSIX兼容的操作系统,其中PHP已posix安装了扩展名。我不知道有类似Windoze的东西。