In my local development environment, I have Apache and PHP installed on Windows 7. I'm calling 7-Zip from my PHP program with exec. I tried at first with
exec('7z a example.zip example.pdf');
but it didn't create the zip file. After checking the Apache error log, I found
'7z' is not recognized as an internal or external command, operable program or batch file.
After changing the exec
to include the full path to 7-Zip.exe, it worked.
exec('"C:\\Program Files\\7-Zip\\7z" a example.zip example.pdf');
But C:\Program Files\7-Zip
is included in my Windows system PATH. The same PHP code works from the command line without using the full path.
php -r "exec('7z a example.zip example.pdf');"
Why is it requiring the full path when I use it with Apache?
An important point which I neglected to include when I originally posted this question is that I am already able to use exec()
to call other programs included in the Windows System PATH without referring to them by their full paths.
Another point which I did not mention originally because I did not realize its relevance was that 7-Zip had been added to the PATH only recently, and I had restarted the Apache service after adding it.
I've WAMP installed on windows 8 and after reading your question I decided to test a couple of things.
Running echo exec('whoami');
echoed:
nt authority\system
This confirms what @Barmar said, Apache isn't running under the same user as you, so, the PATH
is different.
I decided to stop Apache and start it manually under the Administrator account. Then I tried:
echo exec('whoami');
Which outputted:
computername\administrator
I assumed that now the exec
would work with PATH
and tried:
echo exec('adb');
//android adb tool is on my PATH
Surprisingly, despite the fact Apache was running with the same user as me, the PATH
still didn't work. I've no idea why this is happening and if someone has a clue please comment below.
I managed to use the PATH
(using the Administrator account) with the following code:
https://stackoverflow.com/users/171318/hek2mgl $WshShell = new COM("WScript.Shell"); $oExec = $WshShell->Run("cmd /C 7z a example.zip example.pdf", 0); // 0 invisible / 1 visible
I didn't test the code below, but you can try setting the PATH
under the Apache Service account (nt authority\system
), and then use the command, i.e.:
echo exec('set PATH=%PATH%;C:/path/to/7z');
echo exec('7z a example.zip example.pdf');
I believe the path
will still be valid between restarts.
this answer, may help you setting the PATH
for the account nt authority\system
.
The Local System user's personal environment variables are specified at "HKEY_USERS.DEFAULT\Environment". The machine-wide environment variables are specified at "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment". The former isn't really easily accessible from anywhere except the registry, but the latter is accessible from the "Environment Variables" dialog on the "Advanced" tab of the "System Properties".
For future users, the correct way to set the Apache PATH is:
You can use
setEnv
in.htaccess
orputenv
inPHP
code to set$PATH
Credit goes to hek2mgl
I just figured out what was causing this problem. It was actually unrelated to my original assumption.
I remembered seeing PATH
information in phpinfo()
, so I looked at that. In the "Apache Environment" section it did show all of the PATH
except the path to 7-Zip, which I had just added to the system PATH
recently. So apparently it DOES seem to have access to that path, but it wasn't using the current version of it. Why not?
Normally I would think I had just forgotten to restart Apache after updating the path, but I'd restarted it repeatedly while trying to figure this out. But apparently restarting Apache does not refresh this value. I had to stop it and then start it. Then the 7-Zip path showed up in PATH
in phpinfo, and I was able to change my program back to using plain 7z
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With