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

Posted on 2006-05-18
Last Modified: 2013-12-13

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
Question by:anneaquilino
    LVL 14

    Accepted Solution

    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.
    LVL 14

    Expert Comment

    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.
    LVL 40

    Assisted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now