0

I've been busting my head against the wall on this one for hours and I can't make any progress.

Right now I've simmered down my problem to the simple (maybe?) task of trying to get a PHP script to run a .bat program, on localhost. I'm using WAMP.

This is my current PHP file:

<?php system('cmd /c C:\Users\user\Desktop\open.bat'); ?>

That's all there is in it. My .bat file, which is on the desktop, contains:

notepad.exe C:\Users\user\Desktop\test.txt

And then I have a simple text file called test.txt on the desktop. I can run the .bat file from the command line and it works fine, but nothing from within the PHP.

There's numerous other threads here asking how to run a .bat from PHP (ex: How do you run a .bat file from PHP?), and I've tried pretty much every technique I've read about online, and NOTHING WORKS.

Ex, I've tried

exec('open.bat') (with the .bat in the same directory), shell_exec(), changing around the locations of files, paths, I really don't know what's up with it. I'm not running PHP in safe mode.

Perhaps there's some configuration that I should know about that will allow it to run? Or maybe I'm missing something painfully obvious...

Community
  • 1
  • 1
Nik
  • 312
  • 1
  • 4
  • 12
  • Any errors while execution? Or just blank result? – veritas Dec 22 '12 at 07:28
  • Just blank result; no errors in the console or anywhere else I can see. – Nik Dec 22 '12 at 07:39
  • *note* I tried putting the .bat in the same directory as the PHP file, and changing its contents to `exec('open.bat');` - it now is stuck 'waiting for localhost' if that changes anything... – Nik Dec 22 '12 at 08:00

3 Answers3

1

It's possible that you don't see Notepad run because it's an interactive program and the web server (which is trying to start it) runs in a different user session than the one you are in. Doing the same from the command line (e.g. with php -f) would work in that case.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • in which case shellrunas might help you: http://technet.microsoft.com/en-us/sysinternals/cc300361.aspx – ryuusenshi Dec 22 '12 at 19:21
  • I just discovered a setting in Apache to 'allow Apache to communicate with the desktop' - after setting that and restarting WAMP, the .bat at least runs! – Nik Dec 22 '12 at 19:46
  • so I think it's running under the correct user.. However, it now opens up 'interactive services detection' (a Windows utility), which tells me that 'a program running on this computer is trying to display a message', which may have happened because 'the program may not be compatible with Windows'. When I click 'view the message', it exits Windows Explorer and takes me to a blank blue screen with the Notepad file on it - so it opens! But in a really weird way. I really am not quite sure what's going on with it.. – Nik Dec 22 '12 at 19:49
  • @Nik: The user that Apache runs as has a separate desktop, which is what you see. I 'm not sure about the fine print here, but did you look at that shellrunas link? – Jon Dec 22 '12 at 20:22
  • Ok I'm trying it with shellrunas - the interactive services detection still opens, only now shellrunas is running in it rather than the .bat – Nik Dec 22 '12 at 20:56
  • It seems like there's some setting within Windows or Apache that won't allow me to run anything from within my PHP files.. – Nik Dec 22 '12 at 20:57
0

you could try using start /b C:\Users\user\Desktop\open.bat instead of cmd

also you might have to escape the slashes, like so \\ instead of \

So, I suggest you try:

<?php system('start /b C:\\Users\\user\\Desktop\\open.bat'); ?>

ryuusenshi
  • 1,976
  • 13
  • 15
0

Try This....

<?php
$cmd = C:\Users\user\Desktop\open.bat;
function execInBackground($cmd) {
   if (substr(php_uname(), 0, 7) == "Windows"){
       pclose(popen("start /B ". $cmd, "r")); 
   }
   else {
       exec($cmd . " > /dev/null &");  
   }
}
execInBackground($cmd);
?>

For more refer: http://php.net/manual/en/function.exec.php

Cirrus xxx
  • 11
  • 3