I've installed PHP with the Apache module and it's working fine. I can do all sorts of whiz-bang stuff, create all the "Hello World" conditional pages you could imagine. However, there's something I've been beating my head against the wall about: I need to call a command-line program from a PHP page. It's just that simple. I can't get anything to work, even a simple directory listing. I've tried using both System() and Exec() and it always fails. I get an "Unable to Fork [foo.exe]" message each and every time. I've tried every possible variation and cannot get it to work: please help?

My syntax:
<?php
Exec("dir /D");
?>

OR

<?php
Exec("cmd dir /D");
?>

My syntax:
<?php
System("dir /D");
?>

OR

<?php
System("cmd dir /D");
?>

I'm sure I'm blowing something from a syntax standpoint, so any clue someone could give me would be grand. Thanks for your help: my configuration info follows.


Configuration:

  • Windows NT 4.0 / SP 6
  • Apache 1.3.14
  • PHP 4.0.4pl1
  • php4_module
  • Apache Root is D:\Apache
  • PHP Root is D:\PHP

From my httpd.conf:

LoadModule php4_module D:/php/sapi/php4apache.dll

AddType application/x-httpd-php .php


From my php.ini (At C:\winnt):

extension_dir = D:\php\extensions\


P.S. I've seen about 7000 posts on this subject, and have tried all of the answers people give. I'm not posting this to simply be a pest and throw another one on the pile. 🙂

    Heh heh....my syntax in my examples SHOULD read "dir D:" NOT dir "dir /D". My bad.

      7 months later

      Andy !

      I have been trying for weeks to exec another script with the exec function within PHP but exec just won't work at all.

      Have you discovered the secret yet ?

      I posted the following question but have yet to hear from anyone who knows the answer:
      Must exec() be enabled/switched on when compiling PHP as with XML or Sockets ?

      Peter M

        16 days later

        What version of PHP are you using???? The bugs have been worked out in PHP 4.0.6 . I had the same problems with Windows, but they were solved with the upgrade.
        Download the windows self installer and enjoy. http://www.php.net/

          8 days later

          Got the whole latest stuff on my comp; still doesn't work. Config is the same as Andy's one. A man here http://aspn.activestate.com/ASPN/Mail/Message/php-Dev/686680 reports that that issue still - exec/system on win - still not fixed in 4.0.6. So, Peter, if you have some special secret, PLEASE share your knowledge with us.

            I have been able to exec another PHP script in the following manner:

            exec("/usr/bin/php -q ./test2.php '$line1 $line2 $line3 $footer'");

            I also have been able to use the system function as follows PHP to fire PGP:

            system("/usr/local/bin/pgp -at $clear $keyName -o $crypted >/dev/null 2>&1");

            There appear to be some PHP verion/configuration and op system issues with the exec() function and perhaps with system() as well, but that way above my head.

            Hope this helps,
            Peter

              16 days later

              I installed the phpdev bundle on Win2K with PHP 4.06 and I get the unable to fork message too.

              Seems like it has not been fixed in this version.

              Cheers, Chris.

                20 days later

                I think they didn't test it with internal shell commands like dir!

                  6 months later

                  I am banging my head into the same problem. Win98se
                  php 4.0.6
                  php.ini directive safe_mode=Off "recommended by fourm in php.net"
                  php.ini directive safe_mode_exec_dir=none

                  However, I was able to execute and exec() command when the the executable was located in the $DOCUMENT_ROOT directory (php page directory). This does me no good. Advice any one?

                  Charlie

                    15 days later

                    Has anyone found a solution to this problem yet - i know these are all old posts, so maybe you can't remember the hell you went through - but i'm going through it now!!
                    I can't get exec() to work, i have php v.2.1.0 and windows XP, IIS v.5. and all i get is unable to fork.
                    So? please help me!!

                      19 days later

                      try to install easyphp from www.easyphp.org version 1.5 not 1.6!

                        2 months later

                        Heya, i'm having the same problems,but i don't get any output, no errors, nothing but a blank page, everything works
                        except the exec and system functions, me win98, apache, php, ssl(no certification), perl, mysql, ms access database... everything works ok, perl does too, i've tryied served parsed .shtml files to execute perl and it does good, i've tryied php(my entire website is in php), i've tryied mySQL(a lot), with ms access databases too, so the only thingy is the exec and system...

                        plz, help us all

                          Write a Reply...