I am facing a puzzle, im testing some scripts on my wamp installation. When i do a echo date('d) and i run the script from the browser i see the correct week day, by example, lets say it echo 'Mon', but.. when i execute it from a batch file, the echo shows the next day, by example 'Tue', why this can be happening? I have the correct timezone in my php.ini the windows clock as well, i dont have a clue why this is happening. In the same puzzle, i have a path to a dbconnect file, again, from browser it run ok, but from the batch file it dont find the included path, i solved it writing the full windows path C:/path/to/file.php but i dont understand why, the include is done by the php file executed by the batch file, so the path should be ok right?
If someone can share some lights i would appreciate it.
In WAMP the CLI uses a separate php.ini file in the PHP installation directory (e.g. C:\wamp\bin\php\php5.3.13) rather than the one under the Apache installation so check the settings in that file for running PHP on the command line.
Using php through the browser is using the CGI/FPM version and with your command line it is the CLI version. CLI and server (cgi/fpm) use different php.ini-files. On cli type
php --ini
To find where your ini file is and correct it.
Related
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.
Update:
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 or putenv in PHP 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.
When I try to use Webception over wamp server on my windows 7,8 the below error appears for each tests
'php' is not recognized as an internal or external command,operable program or batch file
I have already installed php and the path is correct. Does it need more setup? How I can fix this issue ASAP?
This is because windows does not know where to find the php.exe file as it is not in a folder that is on your PATH environment variable.
However, when using WAMPServer it is a VERY BAD IDEA to add the path containing the php.exe to your PATH in the traditional way, because of course in WAMPServer it is possible to have many version of PHP available within one instance of WAMPServer.
The best solution in a WAMPServer environment is to create yourself a little command file that you can run from within a command windows that will add the php path you want to use for this specific project.
So create yourself a .cmd file and place THAT file in a folder that is already on your windows PATH environement variable.
So for example :-
File = phppath.cmd
PATH=%PATH%;c:\wamp\bin\php\php5.5.12
ECHO --------------------------------------------
php -v
ECHO --------------------------------------------
Of course you can make this as clever as you like and accept parameter specifying the php version you want to run, but untill you actually have more than one PHP version installed this is good enough.
I was also having a problem with webception on wamp failing to find php despite it being available on user and system path.
I found that wamp on windows by default does not provide environment variables to PHP.
I fixed this by editing php.ini (in apache/apache/bin/php.ini)
and ensuring that the following is set
variables_order = "EGPCS"
As at the current webception version 0.1.0 there is a bug to do with path generation that fails tests because the test file cannot be found. There is a solution for this at https://github.com/syntithenai/Webception/
This is driving me nuts.
I've reduced my problem to this: I have a simple file 'test.php' that invokes a php file (this is from a bigger application, so in fact we need to execute many php scripts from a main script):
<?php
exec("php /var/www/setActive.php 273 1 2>&1",$arO,$nO);
echo $nO.'|'.implode(',',$arO);
The original path for setActive.php is way longer, I shortened it for this example.
Now, if I try:
[root#stg]# sudo -u apache php test.php
I get:
0|
Great, this means the setActive.php script ran smoothly.
Now, if I try it from a browser or from curl:
[root#stg]#curl http://my.url/path/test.php
This is what I get:
1|Could not open input file: /var/www/setActive.php
I already reviewed all the permissions. Apache user has access to all directories and files.
This was working ok in another server. I reviewed the php.ini files and there is no difference between them.
Maybe is a option that I'm not seeing, or an apache config option. I'm ran out of ideas.
Please help me!
Thanks in advance.
I have Linux, Cent OS. PHP 5.3.23
If permissions etc.. are all ok, possibly the apache config. in your setup has the 'ChrootDir' set:
http://httpd.apache.org/docs/2.2/mod/mpm_common.html#chrootdir
This uses the syscall chroot() and the view for '/' dir for that process starts from this setup dir, and hence everything should be relative to it.
My setup is as follows: Windows 7, XAMPP with Apache and PHP enabled I have a PHP script in which I call an external program to do run a conversion. This external program is an EXE file, which requires 3 attributes:
The source file
The destination file
Additional flags (conversion type etc)
When I use the command line tool built into XAMPP to execute my script, everything works fine. But when I use the exec() function in my PHP script, no output file is created. I'm pretty sure the conversion is actually happening (it takes about 5 seconds, about the same time it takes to run the PHP script).
I think it's a permissions thing, so I already moved the EXE file to the same folder as my PHP file and adjusted the permissions of the entire folder (I granted all permissions to all users). I also disabled the Windows UAC and tried to put the command in a BAT file. The file just is not created.
Any help or tips would be greatly appreciated!
My PHP code is as follows:
exec('c:\converter.exe c:\src.txt c:\dst.txt -f', $output);
print_r($output);
When I print out $output, the array turns out to be empty. When I put the exact same command in Command Prompt, the code works like a charm (no syntax errors). I use absolute paths as well.
Try to copy your executable file in same folder as your application.
try
exec("script.exe src.txt dst.txt", &$output);
echo $output;
also, do not forget to use escapeshellcmd() to add some security to your application.
Thank you very much for your input! As it turns out, it was Windows issue caused by the 'Interactive Services Detection' feature. Apache was running as a system service, which prevented calls to external programs (with a GUI). I disabled the run-as-service feature in XAMPP, which solved the problem. A more thorough explanation can be found here: http://php.net/manual/en/book.exec.php
I have a web accessible PHP script that is using a shell command to drop PDFs to text. I installed Poppler, and am using pdftotext, via MacPorts. I am able to run the command successfully from the CL, and when supplying the full path within the PHP script to '/opt/local/bin/pdftotext'. So, I know that my $PATH is correct and the permissions are sufficient, yet I am still getting an exit status of 127: Command Not Found, when attempting to do simply 'pdftotext' in the exec().
I have tried the answers from How do I add paths to the Apache PATH variable? and http://lists.apple.com/archives/macos-x-server/2008/Sep/msg00433.html. I modified both the /etc/paths and /etc/profile, and added /etc/paths.d/macports all pointing to '/opt/local/bin'. setenv, apache_setenv, etc all had no effect also.
I am using a MAMP (1.9 I think) install for my local development, OSX 10.6, PHP 5.3.5, all a little behind I know :-) ... my $PATH is modified to point to the MAMP bin/php
/etc/paths.d/macports will influence on PATH variable for macports, not for the Apache. You probably need to add /etc/parhs.d/apache (or else) to do what you need.
Edit: also check this and this threads for solutions. It is somewhat outdated but still can help.