how to use exec()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gauri
    New Member
    • Jun 2006
    • 3

    how to use exec()

    Hi
    I want to execute a command on the cmd prompt thru php..

    Exp: I want to execute 'ls' and store the list in a file.
    I tried using exec() function but it returns 1..
    not able to see the list.

    how to go about it?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I imagine that as a security precaution this may have been disabled.

    Comment

    • dbunny
      New Member
      • Jun 2006
      • 5

      #3
      Try

      "which php"

      If it doesn't find it, then look in /usr/local/bin/ or /usr/bin

      If you're on windows, gotta find the directory for PHP and add it to your PATH env varible.

      Other wise, you just do

      php -f thing.php

      and thing.php is

      #!/usr/local/bin/php
      <?php
      system("ls -l");
      ?>

      What platform are you on?

      Comment

      • gauri
        New Member
        • Jun 2006
        • 3

        #4
        I am working on windows platform..
        I am using WAMP.


        I want to execute a command on command prompt and redirect its output to a File..
        How to go about it??

        Comment

        • sashi
          Recognized Expert Top Contributor
          • Jun 2006
          • 1749

          #5
          Hi Gauri,

          first off all check the system environment variable for the PHP path.. since ur working of WAMP the environment variable should have been created by default provided u have installed .msi version.. btw which version of WAMP did you install??

          another shortcut to find the PATH is.. goto ur command prompt n type the command PATH.. see what you get n make sure the PHP path is listed there as well..

          then to execute as command.. lets assume.. save the below as notepad.php

          <?php
          passthru("c:\wi ndows\system32\ notepad.exe");
          ?>

          then from the cmd prompt.. type..

          c:\path-to-php\php.exe c:\path-to-the-notepad.php

          tips of the day..

          the shell_exec() command can also be used.. this command is more suitable when it comes to executing shell command.. :)

          good luck..
          Last edited by sashi; Jun 19 '06, 02:02 PM.

          Comment

          Working...