问题描述
致我的 PH 人民,
这是一个奇怪的 PHPuzzle,但我想知道它是否是 PHPossible.
This is sort of a weird PHPuzzle, but I'm wondering if it's PHPossible.
最终目标是与
mysql mydb < file.sql
但是有了这样的 API
But with an API like this
./restore < file.sql
其中 restore
是这样的 PHP 脚本
Where restore
is a PHP script like this
#!/usr/bin/env php
$cmd = "msyql mydb";
passthru($cmd, $status);
但是,我想将 STDIN 传递给 passthru
命令.
However, I want to pass STDIN to the passthru
command.
这里的明显好处是我可以将 restore
放在管道中的某个位置,并且一切正常.举个例子
The clear benefit here is that I can put restore
somewhere in a pipeline and everything works peachy. Here's an example
# would be pretty awesome!
ssh $remote "msyqldump $config mydb | gzip" | gzip -dc | ./restore
无论如何,我怀疑是否可以使用 passthru
,但也许以某种方式使用 proc_open
?
Anyway, I doubt it's possible using passthru
, but perhaps with proc_open
in some way?
作为最后的手段,如果出现无法解决的 PHPredicament,我会做这样的事情
As a last case resort, in the event of an unsolvable PHPredicament, I would do something like this
./restore file.sql
像这样的脚本
#!/usr/bin/env php
$cmd = sprintf("mysql mydb < %s", $argv[1]);
passthru($cmd, $status);
推荐答案
什么!PHP发现!
结果 passthru
已经做到了!现象P!
Turns out passthru
already does this! PHenomenalP!
看看吧,PHPal:
gzip.php
#!/usr/bin/env php
<?php
passthru("gzip", $status);
if ($status !== 0) {
error_log("gzip exited with status %d", $status);
}
多么PHProgressivist!
How PHProgressivist!
echo "hello" | php gzip.php | gzip -dc
输出
hello
这篇关于如何通过管道将 STDIN 传输到 PHP 生成的 proc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!