简体   繁体   English

在 PHP 页面中打印 .out 文件的输出

[英]Print output from .out file in a PHP page

I'm having a hard time putting the output from an executable on my web server into a web page.我很难将 Web 服务器上的可执行文件的输出放入网页中。 I'm hoping to get a simple "Hello World" going.我希望得到一个简单的“Hello World”。

Specifically, I have a very basic C program that prints "Hello World".具体来说,我有一个非常基本的 C 程序,可以打印“Hello World”。 I've compiled it using gcc, which produces HelloWorld.out, which I can run from the command line.我使用 gcc 编译它,它生成 HelloWorld.out,我可以从命令行运行它。

I then have a basic index.php file, in which I am attempting to use passthru() to output the result of the C program into the web page.然后我有一个基本的 index.php 文件,我试图在其中使用 passthru() 将 C 程序的结果输出到网页中。 The .php file looks like: .php 文件如下所示:

<?php passthru('HelloWorld.out'); ?>

I've uploaded both the php and the .out file to the same directory on my server.我已将 php 和 .out 文件上传到服务器上的同一目录。 However, index.php produces no output.但是,index.php 不产生任何输出。 I've looked around online and there's a few places I may be getting this wrong... My general goal is to be able to embed the output from an executable program on my web server into the page (doesn't need to be HelloWorld.out; possibly the .out extension doesn't work on the server?).我在网上环顾四周,有几个地方我可能会出错......我的总体目标是能够将我的网络服务器上的可执行程序的输出嵌入到页面中(不需要是 HelloWorld .out;可能 .out 扩展名在服务器上不起作用?)。 Or I may be using passthru() incorrectly;或者我可能错误地使用了 passthru(); as I understand this is the most barebones was to use it.据我所知,这是使用它的最准系统。

Thanks!谢谢!

When you tell the operating system to run a particular executable without an explicit path it will use the PATH environment variable (colon separated set of paths) to find it.当您告诉操作系统在没有显式路径的情况下运行特定的可执行文件时,它将使用PATH环境变量(以冒号分隔的路径集)来查找它。

Typical paths would be /bin , /usr/bin/ , etc.典型的路径是/bin/usr/bin/等。

If the executable can't be found in those paths, it will throw an error and give up.如果在这些路径中找不到可执行文件,它将抛出错误并放弃。 To fix this, you have these choices:要解决此问题,您有以下选择:

  1. Specify the absolute path to the executable (ie /path/to/HelloWorld.out )指定可执行文件的绝对路径(即/path/to/HelloWorld.out

  2. Specify a path relative to the current directory (ie ./HelloWorld.out )指定相对于当前目录的路径(即./HelloWorld.out

  3. Use PHP to create an absolute path:使用PHP创建绝对路径:

    passthru(__DIR__ . '/HelloWorld.out');

Or:或者:

    passthru(dirname(__FILE__) . '/HelloWorld.out');

Lastly, the file you wish to execute must be executable, either using chmod to change the permissions or by passing the path of the file to sh .最后,您希望执行的文件必须是可执行的,可以使用chmod更改权限或将文件路径传递给sh

passthru('/bin/sh ' . dirname(__FILE__) . '/HellWorld.out');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM