How to run a batch file from PHP...exec() and system() not working.


I am trying to run a batch file from PHP.  It works fine when run manually but when I try to call it from within PHP it doesn't run.  I've tried with exec() and system() with no luck.  Here is my code:

system("\\qcwbfxd1\webdata\fxhome\fx\chartupdate\convertimage.bat", $output);


exec("\\qcwbfxd1\webdata\fxhome\fx\chartupdate\convertimage.bat", $output);

$output returns a 1 in both cases.

Any help would be greatly appreciated.

Thanks, Anne
Who is Participating?
This is probably a permissions issue.

The "qcwbfxd1" server must have execute and read permissions to the "IUSR_<webserver_machine_name>" user account to those batch files.
Since the batch file needs access to the CMD you need to give it permissions to cmd.exe:

From a dos prompt:
C:\WINDOWS\system32>cacls cmd.exe /E /G ServerName\IUSR_ServerName:R

Here is how to use cacls:

CACLS filename [/T] [/E] [/C] [/G user:perm] [/R user [...]]
               [/P user:perm [...]] [/D user [...]]
   filename      Displays ACLs.
   /T            Changes ACLs of specified files in
                 the current directory and all subdirectories.
   /E            Edit ACL instead of replacing it.
   /C            Continue on access denied errors.
   /G user:perm  Grant specified user access rights.
                 Perm can be: R  Read
                              W  Write
                              C  Change (write)
                              F  Full control
   /R user       Revoke specified user's access rights (only valid with /E).
   /P user:perm  Replace specified user's access rights.
                 Perm can be: N  None
                              R  Read
                              W  Write
                              C  Change (write)
                              F  Full control
   /D user       Deny specified user access.
Wildcards can be used to specify more that one file in a command.
You can specify more than one user in a command.

   CI - Container Inherit.
        The ACE will be inherited by directories.
   OI - Object Inherit.
        The ACE will be inherited by files.
   IO - Inherit Only.
        The ACE does not apply to the current file/directory.
Richard QuadlingSenior Software DeveloperCommented:
Several things.

1 - The use of \ and / is a major issue. Windows requires \ to be used to separate directories. But \ is the escape character, so you need to use \\.

So, before you try anything else, try ...

exec('\\\\qcwbfxd1\\webdata\\fxhome\\fx\\chartupdate\\convertimage.bat', $output);

Yes. That is 4 \ at the beginning.

2 - Some commands and or parameters may contain spaces. Normally you would do something like this from a normal command line.

"C:\Program Files\Some Application\app.exe" -f "C:\data\Some Application\data.txt" > "C:\data\Some Application\output.log"

sort of thing. Program names, parameters, etc with spaces in are enclosed in double quotes. This is normal and what you would do in a .BAT file.

But, there is a problem. When PHP executes an external application via exec, it automatically uses the COMSPEC for the machine. My COMSPEC is C:\Windows\system32\cmd.exe. So far so good.

The issue is with CMD.EXE. Only 1 set of double quotes are allowed.


Create a directory ...

MKDIR "C:\A folder"

Copy notepad into this folder.

COPY C:\WINDOWS\system32\notepad.exe "C:\A folder\np.exe"

Run the new np using the full path

"C:\A folder\np.exe" "C:\A folder\new.txt"

Hopefully, you now have np running with a new file.

Quit np.

Now try to load this using CMD

cmd /c "C:\A folder\np.exe" "C:\A folder\new.txt"

This will fail normally with the message ...

The filename, directory name, or volume label syntax is incorrect.

This means using "ccc" "ppp" in PHP's exec WILL fail.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.