gpt4 book ai didi

php处理<来自命令行的文件输入

转载 作者:行者123 更新时间:2023-12-02 04:01:38 25 4
gpt4 key购买 nike

我知道我可以在命令行/shell 脚本上接收参数,如下所示:

!#/usr/bin/php
<?php
# file name - process.php
print_r($argv);

但是重定向如下:

#> ./process.php < input.txt

如何读取文件,input.txt 是字符串参数还是已创建的某种类型的文件指针?

最佳答案

Read from STDIN与 C 非常相似:

<?php
$stdin = fopen('php://stdin', 'r');
// Get the whole file, line by line:
while (($line = fgets($stdin)) !== FALSE) {
...
}
?>

如果您希望将整个文件内容放入一个变量中,有一个快捷方式:

$contents = stream_get_contents(STDIN);

关于php处理<来自命令行的文件输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41595370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com