dongqishun6409 2018-03-21 14:46
浏览 73
已采纳

如何从PHP脚本中将数据传输到可执行文件中?

I have a binary that takes input from stdin that I use on the command line by piping file contents to it like cat query.sql | go-mysql-format, but how do I pipe a variable to the executable?

Currently I have

file_put_contents($File = "$_SERVER[DOCUMENT_ROOT]/tmp/" . uuid(), $MySQL);
$o = shell_exec('cat ' . escapeshellarg($File) . ' | go-mysql-format --html');

Basically I would like to skip the file creation.

It's also important to note that the data will contain newlines, so I'm not sure wrapping the variable with escapeshellarg will be appropriate

  • 写回答

1条回答 默认 最新

  • duanji7881 2018-03-21 15:05
    关注

    Thanks to the point in the right direction from @NigelRen to look at proc_open

    I wrapped the steps in a function for me to use later, which might be helpful.

    function exec_stdin(string $Command, string $Data) {
        $_ = proc_open($Command, [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $p);
        if (is_resource($_)) {
            fwrite($p[0], $Data);
            fclose($p[0]);
            $o = stream_get_contents($p[1]);
            fclose($p[1]);
    
            $_ = proc_close($_);
    
            return $o;
        }
    
        return false;
    }
    
    var_dump(exec_stdin('go-mysql-format', 'yeet'));
    

    returns

    string(7) "`yeet` "
    

    which is exactly the output I needed!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!
  • ¥15 matlab生成的x1图不趋于稳定,之后的图像是稳定的水平线
  • ¥15 请问华为OD岗位的内部职业发展通道都有哪些,以及各个级别晋升的要求
  • ¥20 微信小程序 canvas 问题
  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验