[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-05-18
5
Medium Priority
?
558 Views
Last Modified: 2013-12-13
Hi,

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);

or

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

$output returns a 1 in both cases.

Any help would be greatly appreciated.

Thanks, Anne
0
Comment
Question by:anneaquilino
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
CtrlAltDl earned 1000 total points
ID: 16713357
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.
0
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 16713394
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.

Abbreviations:
   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.
0
 
LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 1000 total points
ID: 16715803
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.

Proof.

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.

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

873 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