2014-01-20 27 views
0

如何檢查是否

$data = file_get_contents("php://stdin"); 

是空的,就像當一個應該傳遞參數,但確實不適合。



我有一個文件test_cmd.php

#!/usr/bin/php 
<?php 

    $data = file_get_contents("php://stdin"); 
    echo $data; 

當我運行

$ ls -l | test_cmd.php 

我得到

total 36 
drwxrwxr-x 3 username username 4096 Jan 20 11:10 ./ 
drwxr-xr-x 16 username username 4096 Jan 19 23:49 ../ 
-rw-rw-r-- 1 username username 4299 Jan 19 13:06 index.php 
-rwxrwxr-x 1 username username 2619 Jan 20 12:58 test_cmd* 
-rw-rw-r-- 1 username username 3022 Jan 19 10:18 file1.php 
-rw-rw-r-- 1 username username 3022 Jan 19 10:18 file2.php 

然而,當我運行:

$ test_cmd.php 

控制檯等待文本輸入,這是我不想。我想顯示Usage的幫助。

我該如何糾正?

回答

2

嘗試使用posix_isatty

if (posix_isatty(STDIN)) 
{ 
    die("Please don't run me interactively!"); 
} 
else 
{ 
    echo file_get_contents('php://stdin'); 
} 
+0

快速測試似乎工作。我會在一段時間內充分測試它。 – maan81

0

試試這個:

if($filecontent = file_get_contents("php://stdin") !== false) 
{ 
    // do stuff here 
} 
+0

我已經試過了。我得到了和以前一樣的空白行。 – maan81