Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

Thread: command line

  1. #1
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    Is it possible from PHP to run a command in commandprompt?

    I need to run a batch file that is located in my c:\windows folder. Is it possible?

  2.  

  3. #2
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    See the functions on the PHP site, the chapter on Program Execution Functions. The shell_exec function seems particularly pertinent.

  4. #3
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    Thanks for the help, but it doesnt seem to be working correctly.

    I ran this:
    PHP Code:
    $output shell_exec('dir'); 
    and it resulted in
    PHP Code:
    Warningshell_exec() [function.shell-exec]: Unable to execute 'dir' in testcmd.php on line 3 
    any suggestions? Im running on a Windows machine, and the examples at php.net are all Linux based

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Well, the issue is that you might have to actually run it as a batch file for a command. Try something like `cmd dir.bat' or something.

  6. #5
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    Ive created a batch file under my C:\ called test.bat. All it contains is 'dir'

    Ive run it from command line and it works, so I tried both shell_exec('test.bat'); and shell_exec('cmd test.bat'); but both fail. Im sure Im missing something....

  7. #6
    Senior Member
    Join Date
    Aug 2003
    Posts
    444
    Member #
    2801
    You need the full path probably:

    shell_exec("C:/test.bat");

    or

    shell_exec("C:\\test.bat");
    eKstreme
    eKstreme.com - Free website tools!
    fontfox - free fonts Hand-picked quality fonts.

  8. #7
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    nope, still doesnt seem to work.

    Ive also tried system('test.bat'); etc etc, and that doesnt seem to work either.

    any other suggestions?

  9. #8
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    First of all, it occurs to me that you may not be running Windows XP/2000/NT. If this is the case, you need to run command as opposed to cmd. Second of all, you need the full path to both the file and the program. In Windows XP, cmd is located at C:\WINDOWS\system32\cmd.exe . I believe in Windows 9x/Me, command is at C:\WINDOWS\command.exe or command.com, but I'm not sure, that's something you'll have to look up. Then, you would run:
    PHP Code:
    shell_exec"C:\WINDOWS\system32\cmd.exe C:\test.bat" ); 
    That should work.

  10. #9
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    Im still struggling with this. Ive searched the web, but I still cant find something that works. Im running on Windows XP Pro, IIS, and PHP 5.0.2.

    I have a test.bat(its located in the local directory that Im running the PHP script from) file that contains:
    Code:
    dir
    After searching around, Ive found a couple of people using exec() to execute a command. My php file:
    PHP Code:
    echo exec('test.bat');
    exec('test.bat'$output);
    echo 
    $output
    //Im trying both examples to see if one of them works... 
    Im getting an "Unable to fork [test.bat]" error.

    Any help is greatly appreciated....

  11. #10
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    test.bat is not an executable program. Only cmd.exe and command.exe are. They take the batch file as an argument, and process it and run it. You can't execute test.bat, you have to execute cmd with test.bat as an argument, like the example I posted above. Try doing the example I posted except with exec instead of shell_exec if you'd like. The important part in the post was the string you were executing, not the function used.

    EDIT: Oh, and be sure to search for cmd.exe and put in the full path where you find it on your computer. I think I have XP Home, so it might be in C:\WINNT for you.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:54 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com