PHP - exec a c++ program -- problems

Discussion in 'PHP' started by gpfdez, Feb 21, 2009.

  1. #1
    Hi!

    I need to execute a c++ program on a website. Now, I'm proving some simple code to execute a "helloworld " program that gives back a number. I try to print that result with echo but it only prints "Array". The code is:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <HTML>

    <HEAD>

    <TITLE>:: Formulario de carga de ficheros ::</TITLE>

    </HEAD>



    <BODY>



    <FORM NAME="ejecutar" METHOD="POST"

    ACTION="<? echo $PHP_SELF; ?>?ejecutar=1"

    ENCTYPE="multipart/form-data">



    <TABLE WIDTH="80%" STYLE="font-family:Arial;font-size:9pt;">



    <TR>

    <TD ALIGN="LEFT"><INPUT TYPE="SUBMIT" VALUE="EJECUTAR">

    </TR>



    </TABLE>
    <p><? #Aquí realizamos la carga del fichero

    exec('./programa',$result);
    echo $result;



    ?></p>



    </FORM></BODY></HTML>



    The location of the program is on /opt/lampp/htdocs/Ejemplos_Proyecto/nuevo, so I don't know if i have to put something different on "exec". The code of programa is simple:

    #include <iostream>
    int main(int argc,char* argv[]){
    return 0;
    }



    I need some help because it's an important step to go on with the project.

    Thanks a lot.
     
    gpfdez, Feb 21, 2009 IP
  2. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    
    #include <iostream>
    int main(int argc,char* argv[]){
    return 0;
    }
    Code (markup):
    This C++ program does nothing... -_- It will read the vars (e.g.: ./program var1 var2) but it doesn't actually handle them, just returns 0. So why would you need this?

    But the php
    exec();
    PHP:
    command should suit your needs. The thing here to remember though is commands are going to be more or less operating system specific so not the best route if you're using this script across multiple operating systems unless you do some more defining etc.

    NOTE: programa must be in the same directory as your script you're running this from. (As that's what your command is showing) otherwise you need to give the exact path to programa (e.g.: /home/user/programa)

    If you're just getting an "array" message, however, trying putting implode(); around your result.

    Regards,
    Dennis M.

    EDIT:

    I was thinking about this again so I wrote a simple program that will work for you:

    main.cpp
    
    #include <iostream>
    
    int main(int argc,char* argv[])
    {
      std::cout<< argc-1;
      return 0;
    }
    
    
    Code (markup):
    This will return how many different arguments you entered into the program. (E.g.: ./programa a b c d = 4 arguments)

    Now for your PHP:
    
    <?php
    exec('./programa a b c d',$result);
    echo implode($result);
    ?>
    PHP:
    Cheers! :)
     
    Dennis M., Feb 21, 2009 IP