【问题标题】:php exec() not showing expected outputphp exec()没有显示预期的输出
【发布时间】:2021-08-02 13:49:35
【问题描述】:

我正在尝试在 php 上运行 cpp 可执行文件,但它不起作用。请帮忙。

 var_dump(shell_exec("D:\\c_cpp_programs\\string.exe"));
 $output = system('D:\\c_cpp_programs\\string.exe', $retval);
 var_dump($output);
 passthru ('D:\\c_cpp_programs\\string.exe');
 if(file_exists('D:/c_cpp_programs/string.exe'))
    echo "File exist";
 exec('D:/c_cpp_programs/string.exe',$array,$error);
 var_dump($array);
 var_dump($error);

下面是我得到的输出,

C:\wamp64\www\chatclub\application\views\test.php:2:null
C:\wamp64\www\chatclub\application\views\test.php:4:string '' (length=0)
File exist
C:\wamp64\www\chatclub\application\views\test.php:9:
array (size=0)
  empty
C:\wamp64\www\chatclub\application\views\test.php:10:int -1073741515

cpp程序是,

#include<iostream>
using namespace std;

int main(){   
    cout<<"Hello"<<endl;
    return 0;
}

给我 PHP 交互式 shell 的输出,

C:\Users\Shoyeb>php -a
Interactive shell

php > var_dump(shell_exec("D:\\c_cpp_programs\\string.exe"));
string(5) "Hello"
php > var_dump(shell_exec("D:\\c_cpp_programs\\string.exe"));
string(6) "Hello
" <----- this output is with <<endl
php > var_dump(shell_exec("D:\\c_cpp_programs\\string.exe"));
string(5) "Hello"
php >

 

【问题讨论】:

  • 只需查阅手册,其中包含每个函数的示例。这也会让你尝试proc_open()。至于exec(),手册清楚地表明每行尾随空格未被捕获。

标签: php c++ exec


【解决方案1】:

你可以使用:

exec("D:/c_cpp_programs/string.exe", $out);

请注意,$out 将保存输出。
请记住shell_exec 将所有输出流作为字符串返回,但exec 返回输出的最后一行。

我不是 cpp 专家,但 &lt;&lt;endl 可能会输出一个空字符串。我不确定。

【讨论】:

  • 我从 cpp 程序中删除了
【解决方案2】:

当我以管理员权限启动 wamp 时,我终于得到了输出,

C:\wamp64\www\chatclub\application\views\test.php:2:string 'Hello
' (length=6)
Hello
C:\wamp64\www\chatclub\application\views\test.php:4:string 'Hello' (length=5)
Hello File exist
C:\wamp64\www\chatclub\application\views\test.php:9:
array (size=1)
  0 => string 'Hello' (length=5)
C:\wamp64\www\chatclub\application\views\test.php:10:int 0

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 1970-01-01
    • 2014-09-19
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多