I’m having a hard time putting the output from an executable on my web server into a web page. I’m hoping to get a simple “Hello World” going.
Specifically, I have a very basic C program that prints “Hello World”. I’ve compiled it using gcc, which produces HelloWorld.out, which I can run from the command line.
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. The .php file looks like:
<?php passthru('HelloWorld.out'); ?>
I’ve uploaded both the php and the .out file to the same directory on my server. However, index.php produces no output. 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?). Or I may be using passthru() incorrectly; 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
PATHenvironment variable (colon separated set of paths) to find it.Typical paths would be
/bin,/usr/bin/, etc.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:
Specify the absolute path to the executable (i.e.
/path/to/HelloWorld.out)Specify a path relative to the current directory (i.e.
./HelloWorld.out)Use PHP to create an absolute path:
passthru(__DIR__ . '/HelloWorld.out');Or:
Lastly, the file you wish to execute must be executable, either using
chmodto change the permissions or by passing the path of the file tosh.