I am currently working on my own little project, but I have a little problem: I want to set the $PATH environment variable to ./bin, so that when I use exec() and similar functions, it would only search for binary files in that directory (unless I explicitly tell it otherwise).
I have already tried putenv(), which won't work unless I have safe-mode enabled, which I'd prefer not to; and I also tried apache_setenv(), but that didn't seem to work either.
Are there any other solutions I might want to try?
(I am using a Linux machine with PHP 5.3.2)
If you want to set it only in specific circumstances, you can do:
exec("PATH=/my/path ./bin");
The way to alter the PATH used by apache on Mac OS X is described here:
http://lists.apple.com/archives/macos-x-server/2008/Sep/msg00433.html
As stated in that post:
[A]dd the following text into [the file
/System/Library/LaunchDaemons/org.apache.httpd.plist] at the fifth
line:
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin</ string>
</dict>
See the man page for launchd.plist(5) for details on the syntax I'm
using here.
If you need to run your PHP commands as CLI sessions, you'll also
probably need to add /opt/local/bin as a new path under /etc/paths.d
work. For instance, something like this:
shell> sudo echo "/opt/local/bin" >> /etc/paths.d/macports
See the man page for path_helper(8).
instead of setting the path to bin and calling foo, why don't you just explicitly invoke bin/foo?
If You have path set for Yours user AND if Your scripts run as Yours user, only thing You should do, is to set up this path for Yours shell, but is Your's scripts run as ie. apache user (www-data in debian-like systems) for this to work, You should set this PATH for that user explicte
Related
In my PHP file, I ran var_dump(exec('echo $PATH')); and got /usr/bin:/bin:/usr/sbin:/sbin
Then I ran echo $PATHin terminal, I got /Library/PostgreSQL/9.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
How can't I add $PATH variable to Xampp?
Your $PATH represents folders that the system is already aware of, and can access the contents directly. That is all it does. This is a system level thing, not a PHP thing, but anything in your $PATH is already available to PHP.
You should be aware that Mac OSX and Linux both run on a UNIX command line, and it works exactly the same on both.
Say for example I have a directory like:
/usr/local/foobar
I want to use foobar for something. If it is not in my $PATH, then I have to access it with
/usr/local/foobar
If it is in my $PATH, then I can access it with
foobar
The $PATH is separated by : so if my $PATH is /usr/local:/usr/bin, then I have two folders that are known to the system automatically, being:
/usr/local
/usr/bin
And anything in those can be directly accessed.
If I want a list of them in PHP, I would use the following to get a list of the folders:
$path = explode(':', `echo $PATH`);
On the command line, you just do echo $PATH
If you need to add a new folder to it, you would do (from the command line) either:
export $PATH=$PATH:/path/to/new/folder
or
export $PATH=/path/to/new/folder:$PATH
I would use the first one if I want the new folder to be at the end of the results, which would be the case if I were overriding an existing value in it with something else I want it to use instead. I would use the second option if I want my new folder to be put at the beginning of the list.
DO NOT FORGET TO PREFIX OR SUFFIX THE NEW VALUE WITH $PATH, OR YOU WILL LOSE EVERYTHING YOU ALREADY HAD AND PROBABLY BORK YOUR SYSTEM
If you add stuff to the $PATH, you also need to source your .bashrc file to tell the system to reload the path and be aware of the changes, like this:
source ~/.bashrc
The reason your path is different, is because from the command line, you are seeing the $PATH for your user, and from the script, you are seeing the $PATH available to the user the webserver is running under. To resolve this, you have two options:
Run the webserver as your user (which is a bad idea for security reasons, so I'm not going to bother explaining how to do this)
Edit the .bashrc for the user that the webserver runs under (which is a better option)
First, in your script, you want to do
echo `whoami`;
This will tell you what user the webserver is running under.
You can then use sudo su webservername (use the value you got from the above command in place of webservername), then use the above export $PATH=... and source ~/.bashrc to fix the path of the webserver account as needed, then exit to go back to your own account, or just close the shell window. Then go back to your script and do
var_dump( explode(':', `echo $PATH`) );
and you should see the correct values (in addition to whatever other folders the webserver had that you do not on your own $PATH).
You can also use vim or vi to accomplish this, by doing
vi ~/.bashrc
and put one folder per line in that file, then save and close, and do source ~/.bashrc as you normally would to reload it. If you are not familiar with vim, use the export approach instead.
I've been trying and searching about a small issue I have in our php application. We use a which to find the full path of a program, however it returns with
which: no bla in ((null))
on Centos, on our own debian boxes it works just fine. I've figured out since then that (obviously) the PATH is not available in the shell from PHP. But what I can't find out is why that is the case. I've replaced the command with all sorts of commands to find out what the environment is in which I run in.
If I run an echo $PATH I actually see the directories I've set in the .bash_profile. If I run echo $SHELL I know I am using bash, if I run whoami I found out I am not the apache user but a different user, whom I changed the .bash_profile for. As I've read there's a difference between shells, so I thought it might not load the .bash_profile so I've also added the export PATH to the .bashrc.
I can get it to work with a change in the code, I could replace the which, but I am just genuinely interested in why this is not working on this centos configuration. I've now added an export PATH in the exec function before the which and it works, and should also work on other systems, but still I think it shouldn't be necessary.
Anyone know what could cause this behaviour?
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.
I have two remote servers that need to have a certain codebase in sync. I have it all set up on the first, but while setting it up on the second, I ran into a problem:
They don't have hg installed in the same location. I can't just run hg <command> because it returns the error:
sh: hg: command not found
So I had been using the full path, but that won't work with two separate hg locations.
I thought it would be clever to run which hg and use the path from the response, so here's the new code:
$hg = trim(`which hg`, "\n");
$output = `{$hg} pull -u`
But $hg is NULL, so that doesn't work! I can ssh into the 2nd server and see that which hg definitely does work. I even appended 2>> path/to/log to see if there were any errors with the which command, but there was not. I made sure it was writing to the log, so it wasn't related to that.
I am not running in safe mode, and I am definitely allowed to run shell_exec because other commands work.
I know I could just create a symlink so they both had the same path and just hardcode the path, but it's driving me crazy why shell_exec('which hg') isn't working!!!
which hg will only work if hg is already in your path, otherwise it will return the empty string.
Obviously this is not what you want, since if it were already in your path, you could just use hg.
If you're using many machines with different configs, you could just create a bin directory in your home directory, add it to your path and make links to the commands you need in that directory. That will allow you to use the same commands on all machines.
For PHP, you'd need to put it in a directory accessible to the web server (but preferably outside the document root), besides that, you can use the same technique there by giving an absolute path to the common directory where you created your links.
try
shell_exec('/usr/bin/which hg');
instead. $PATH may not be set and that is a typical location of which
Normally google is my friend for these kind of newbie problems, and I'm pretty proud of myself learning as I go without really needing to ask any questions in terms of PHP stuff, but this one's got me stumped. Trying to install a version of PEAR that supersedes my host's copy, which is hideously outdated. Apparently "pear's binary (bin) directory should be in your PATH variable." I don't know what that means or how to edit it, and supplementary to that, wether that will actually solve my problem of an outdated version of pear being on my root server. Any advice in either of these areas would be greatly welcomed, thank you.
Actually, they are talking about the OS's PATH environment variable, not PHP's include path (binaries [bin] are run by the OS, not parsed by PHP) Unfortunately, since you are in a shared hosting environment, you cannot change this environment variable in a permanent fashion. If you do have shell access though, you can modify your .profile file set the PATH variable.
You can use getenv() and putenv() to retrieve and set the PATH variable, but this will be reset on each script run.
That said, you do not need the PATH variables set to use PEAR. If you have a PEAR install on your development computer, you can upload the pear folder onto your host and modify the include_path at runtime to point to your own "install" using set_include_path()
$pearInstallPath = realpath('./pear/packages');
set_include_path('.' . PATH_SEPARATOR . $pearInstallPath);
The PATH variable that's being referred to doesn't actually have anything to do with PHP.
The PATH is the list of directories that your shell will look in to find a command you run on the command line. So, this is talking about making the shell find the right path when you run pear on the command line.
Assuming you're using bash, one way to change this is to add a line like
export PATH=/path/to/pear/bin:$PATH
to a .bash_profile or .profile file in your home directory.
Try this
getenv('PATH'); // for get PATH varibale
putenv('PATH=./'); // for set path variable
getenv, putenv