1

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!

3
  • Doesn't passthru return nothing, so you obviously have no output on the page? void passthru ( string $command [, int &$return_var ] ) php.net/manual/en/function.passthru.php Or am I missing something? Commented Nov 16, 2012 at 8:12
  • @Oleg - " Execute an external program and display raw output" Commented Nov 16, 2012 at 8:12
  • True. Brain not functioning without coffee... Commented Nov 16, 2012 at 8:15

1 Answer 1

2

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.

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:

  1. Specify the absolute path to the executable (i.e. /path/to/HelloWorld.out)

  2. Specify a path relative to the current directory (i.e. ./HelloWorld.out)

  3. Use PHP to create an absolute path:

    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.

passthru('/bin/sh ' . dirname(__FILE__) . '/HellWorld.out');
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Jack, thanks for the response! I'm still not getting any output, however... I've tried passthru(./HelloWorld.out');, passthru('/public_html/test/HelloWorld.out');, passthru(__DIR__ . '/public_html/test/HelloWorld.out');, and passthru(dirname(__FILE__) . '/public_html/test/HelloWorld.out');, none are producing any output..
Nevermind I got it working. - my second issue (foolishly) was simply that I forgot to change the permissions of HelloWorld.out to be executable... duh. You are right about the path though, even with the correct permissions it wouldn't have worked with my initial setup. Currently passthru('./HelloWorld.out'); is doing the trick. Thanks!
Forgot about the possibility that it may not have the x permissions :) added

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.