I have a script in which I am trying to load a custom php.ini file. The script is run on *nix systems via a #!/usr/bin/php -qc /path/to/php.ini header. When doing this, however, PHP reports that the loaded php.ini file does not exist, i.e. none is loaded.
If I execute php -qc /path/to/php.ini /path/to/script in the command line directly, it picks up the php.ini -- is it possible to override the php.ini file using the #! notation?
PHP does not like parsing arguments from the shebang. It only allows one to be present. You can however trick it by omitting the space for the first argument parameter:
#!/usr/bin/php -qc/etc/php5/my.ini
(Obviously this method only works for one such parameter with concatenated argument.)
You can workaround this shebang portability specification fail by wrapping your PHP script in a Shell script:
#!/bin/sh
SCRIPT_PATH="$(dirname $0)"
/usr/bin/env php -qc /path/to/php.ini -f $SCRIPT_PATH/my_original_script.php
Related
How to run a PHP script with command line globally, like:
update var1=abc var2 var3=def
"Update" located in fact: "C:\Localhost\Scripts\Update.php"
Many sources suggest this as:
php update.php var1=abc var2 var3=def
Shortly: How to run a PHP script as if it was a registered executable?
Disclaimer: I've written this answer for Linux/Ubuntu, I'm not sure if it'll work for Windows (the OP updated the question's requirements).
So, this is possible, the way I've described here basically just has a global bash script call a PHP file. Here's the steps:
Make a new bash script:
#!/usr/bin/env bash
php /path/to/yourphpfile.php
Change the file executable: chmod +x /path/to/yourbashscript
Copy the bash script into /usr/local/bin/: sudo cp /path/to/yourbashscript /usr/local/bin/
Open a new terminal, then run yourbashscript (or whatever you named it).
And voila! A globally available PHP script!
I finally solved it:
-Create a .bat file where your php file in, it contains:
#ECHO OFF
php %~dp0/update.php %*
-And save it as "update.bat"
-Add the directory name into "Path" in "Environment Variables"
Now you can write "update var1=abc var2 var3=def" freely in command prompt!
I'm having a little issue with adding shebang #! with my php script on RedHat linux. I have a small piece of test code with shebang added (I've tried different variations as well), but I get the following error message everytime I try to run the script.
Error msg:
-bash: script.php: command not found
Test script:
#!/bin/env php
<?php echo "test"; ?>
Shebang #! variations:
#!/usr/bin/php
#!/usr/bin/env php
It should (for most systems) be #!/usr/bin/env php, but your error isn't related to that.
-bash: script.php: command not found
It says that script.php is not found.
If the problem was the shebang line then the error would say something like:
bash: script.php: /usr/env: bad interpreter: No such file or directory
Presumably, you are typing script.php and the file is not in a directory on your $PATH or is not executable.
Make it executable: chmod +x script.php.
Type the path to it instead of just the filename, if it is in the current directory then: ./script.php.
Instead of 2, you can move/copy/symlink the file to somewhere listed in $PATH or modify the $PATH to include the directory containing the script.
If you script is not located in your /usr/local/bin and is executable, you have to prefix calling your script with php like this:
php myscrip.php
For shebangs, here is what I use:
Like this:
#!/usr/bin/php
or this:
#!/usr/bin/env php
In reply to #NVRM's comment regarding only single use of -d, this is not true.
Start with a chmod +x script as
#!/usr/bin/php
<?php
phpinfo();
and run script | grep -E 'memory_limit|error_reporting', and you'll see
error_reporting => no value => no value
memory_limit => 128M => 128M
Now add some -d entries so you have
#!/usr/bin/php -d memory_limit=2G -d error_reporting=-1
<?php
phpinfo();
and re-run script | grep -E 'memory_limit|error_reporting', and you'll now see
error_reporting => -1 => -1
memory_limit => 2G => 2G
Thus demonstrating you can set multiple options.
In fact, the entire command line is what you are working with here. So you can load extensions, use a different config, etc., everything you can do at the command line.
Leaving here some little notes:
To use a php binary located inside the same folder.
As example a php7.2 executable copied from /usr/bin is in the same path along a hello script.
#!./php7.2
<?php
echo "Hello!";
To run it:
./hello
Which behave just as equal as:
./php7.2 hello
This give portability, but beware of system architectures, the php binary might not match the target platform.
Setting allowed memory from the hashbang:
We can set one INI entry from the hashbang line:
#!/usr/bin/php -d memory_limit=2048M
<?php
phpinfo();
exit;
Then to see if php had understood, using phpinfo():
./myphpProg | grep memory
Correct shell output should contain:
memory_limit => 2048M => 2048M
Doing the above is similar as this command line:
php -d memory_limit=2048M myphpProg.**php**
This is why we can set only one ini value in hashbangs, as php accept only one -d parameter at a time.
find callable shebang for PHP in Linux,
Don't memorize this it, learn how to use it
which php
output
zeus#pop-os:~$ which php
/usr/bin/php
then shebang must be
#!/usr/bin/php
If I try to run this inside a script:
<?php exec("curl http://ww.google.com") ?>
I get:
-bash-3.2$ php test.php
sh: /curl: No such file or directory
using shell_exec:
PHP Warning: shell_exec(): Cannot execute using backquotes in Safe Mode...
How can I run curl as shell command line?
Those errors are happening on Linux, on my mac works.
The issue is that PHP safe mode is on and it is better to use the full path to run cURL (thanks ghostJago and amosrivera). Running the script with the following command fixed the issue:
php -dsafe_mode=Off test.php
I do not want to change the php.ini but it could be a solution too.
shell_exec tells the safe mode problem, but exec just tell you an wrong message, hopefully I tried both exec and shell_exec.
Disable safe mode in your php.ini file. Also check if you do have curl installed.
safe_mode = Off
To convert from an bash command (like you can copy from chrome-dev-tools) to php take a look at this: https://incarnate.github.io/curl-to-php/
at the commandline, do this:
which curl
This will give you the absolute path to the curl program.
Then double check that safe_mode = Off is in your php.ini.
When you've done this, change your code to:
<?php exec("path/you/got/from/which/curl http://www.google.com") ?>
How we run php script using Linux bash?
php file test.php
test.php contains:
<?php echo "hello\n" ?>
From the command line, enter this:
php -f filename.php
Make sure that filename.php both includes and executes the function you want to test. Anything you echo out will appear in the console, including errors.
Be wary that often the php.ini for Apache PHP is different from CLI PHP (command line interface).
Reference: https://secure.php.net/manual/en/features.commandline.usage.php
First of all check to see if your PHP installation supports CLI. Type: php -v. You can execute PHP from the command line in 2 ways:
php yourfile.php
php -r 'print("Hello world");'
There are two ways you can do this. One is the one already mentioned, i.e.:
php -f filename.php
The second option is making the script executable (chmod +x filename.php) and adding the following line to the top of your .php file:
#!/path/to/php
I'm not sure though if a webserver likes this, so if you also want to use the .php file in a website, that might not be the best idea. Still, if you're just writing some kind of script, it is easier to type ./path/to/phpfile.php than having to type php -f /path/to/phpfile.php every time.
Simply this should do:
php test.php
just run in linux terminal to get phpinfo .
php -r 'phpinfo();'
and to run file like index.php
php -f index.php
php -f test.php
See the manual for full details of running PHP from the command line
php test.php
should do it, or
php -f test.php
to be explicit.
I was in need to decode URL in a Bash script. So I decide to use PHP in this way:
$ cat url-decode.sh
#!/bin/bash
URL='url=https%3a%2f%2f1%2fecp%2f'
/usr/bin/php -r '$arg1 = $argv[1];echo rawurldecode($arg1);' "$URL"
Sample output:
$ ./url-decode.sh
url=https://1/ecp/
I'm running php as a shell script.
(I am not sure if "shell script" is correct. The file starts with #!/usr/bin/php.)
This works great. But the MongoDB class doesn't get loaded as the correct php.ini file (having extension=mongo.so) is not used.
How do I make it use that php.ini file?
I already tried #!/usr/bin/php -c /usr/local/lib/php.ini
But I still get the same error - Fatal error: Class 'Mongo' not found
What can be done?
Try putting php.ini in the same folder as the php binary. It seems to look there first.
I know this because I used a very powerful and useful command-line program called strace to show me what's really going on behind my back
$ strace -o strace.log php --version
$ grep php.ini strace.log
Strace digs out kernel (system) calls that your program makes and dumps the output into the file specified after -o
It's easy to use grep to search for occurrences of php.ini in this log. It's pretty obvious looking at the following typical response to see what is going on.
open("/usr/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/etc/php.ini", O_RDONLY) = 3
lstat("/etc/php.ini", {st_mode=S_IFREG|0644, st_size=69105, ...}) = 0
Edit .bashrc located at your home directory and add this line:
alias php='php -c /path-to-custom/php.ini'
In this particular situation. I would
Check to see if the Mongo module is loaded (using extension_loaded() or class_exists())
If not loaded, try to load the Mongo module using dl()
If loading fails, display an error message so the admin can take care of it (STDERR or trigger_error()
Most distributions already ship different versions of php.ini for Web Servers and CLI. Are there other reasons to add another php.ini configuration for script XYZ (in addition to normal configuration)?
I came across this, because I had the same problem. Problem is there is more than one php.ini file used.
The one used by Apache is located in
/etc/php5/apache2/php.ini
This is the one that is modified to run MongoDB with extension=mongo.so.
However, when running a cron job, or from the terminal, it loads a different ini file. You can find this by using the line
grep php.ini strace.log
It is mentioned by thomas-peter.
The path where it displays '=3' is the php.ini file loaded when running the engine from the terminal, this ini file will need "extension=mongo.so" placed in it as well.
The other option is to use a small sh wrapper, something like this below in a myapp.sh file. Don’t forget to chmod +x on the script to run it.
#!/usr/bin/env sh
SOMEVAR='Yea Baby!'
export SOMEVAR
php -c /path/to/my/custom/php.ini /path/to/my/old_script.php
With the added bonus of being able to set or override environment variables pre-run.
This is a bit of a gotcha with PHP.
php -c /path/to/my/custom/php.ini works from the command line.
But try it in a script like this #!/usr/local/bin/php -c /path/to/my/custom/php.ini and your custom php won't load.
The Solution
Get rid of the space between -c and your path:
#!/usr/local/bin/php -c/path/to/my/custom/php.ini