How to Get the Current PHP Executable from Within a Script

How can I get the current PHP executable from within a script?

It is worth noting that now in PHP 5.4+ you can use the predefined constant PHP_BINARY:

PHP_BINARY

Specifies the PHP binary path during script execution. Available since PHP 5.4.

executing a script from a php file

Something like this in /etc/sudoers should work to allow your web server user to run that particular command without issue:

Cmnd_Alias SERIAL = /var/www/html/SCRIPTS/c/jmsend/serial_tx *
www-data ALL=(ALL) NOPASSWD: SERIAL

Note that you must escape user input before using it:

$ric = escapeshellarg($_GET["v"]);
shell_exec("sudo /var/www/html/SCRIPTS/c/jmsend/serial_tx $ric");

You should also be aware of the differences between exec() and shell_exec(), specifically that you can't check for a failure using shell_exec().

$ric = escapeshellarg($_GET["v"]);
exec("sudo /var/www/html/SCRIPTS/c/jmsend/serial_tx $ric", $output, $return);
if ($return !== 0) {
// let the user know something didn't work
}

This assumes, of course, that your executable is written to return appropriate error codes.

Find executable path regardless if it is an alias or a command

If ./test is a shell script, note that your aliases are not available to it.

Unless you really ask it to:

shopt -s expand_aliases
source ~/.bashrc # or whatever file defines your aliases.

php file.php # should now work

However, a simpler method is to add the php directory to the PATH of the script

PATH=/opt/plesk/php/8.0/bin/php:$PATH

php file.php

The generic approach. This assumes the user's shell is bash.

setupTool() {
local tool=$1
if ! [[ $tool =~ ^[[:alnum:]._-]+$ ]]; then
echo "Malicious input: '$tool'" >&2
return 1
fi
case "$( bash -ic "type -t $tool" )" in
alias)
shopt -s expand_aliases
eval "$( bash -ic "alias $tool" )"
;;
function)
eval "$( bash -ic "declare -f $tool" )"
;;
file|builtin|keyword)
: ;; # no-op
*) echo "error: don't know about $tool" >&1
return 1
;;
esac
}

setupTool php || exit 1
php file.php

How can I get the path of the PHP binary from PHP?

You can use:

$_SERVER['_']

Also, the predefined constant PHP_BINDIR gives the directory where the PHP executable is found.

Sample on CodePad and Ideone.

It looks like, for security reasons, $_SERVER values are not exposed.

how to run php script in eclipse

Ok But I do not have php.exe. I only create Test.php in my project.

In order to run **.php* files, you will need PHP installed, and because you already have installed WAMP, you should be fine.

"The current debugger does not have any defined PHP executable."

If you have installed WAMP as you said, try to look for php.exe on your local hard drive and then set path in Eclipse that leads to it (eg. c:\wamp\bin\php\php5.2.9-2\php.exe). Go to Window\Preferences\PHP\PHP Executables and add this path to the list.

How to determine path to php.exe on Windows - search default paths

If the user has added PHP's bin folder to the system PATH, then you should just be able to try and execute php -v to check that it's present.

If you want to obtain the full path to the PHP executable and the target system is Windows Server 2003 or later (so Windows Vista, and Windows 7) then you could use the WHERE command, i.e.:

C:\> where php.exe
C:\Program Files (x86)\WAMP\bin\php\php5.3.5\php.exe

Also see possibly related question: Is there an equivalent of 'which' on the Windows command line?.

If you are really desperate to find any file on the user's computer, you could try executing the equivalent of a find - but it's going to be slooow!

C: && cd \ && dir /s /b php.exe

Convert a PHP script into a stand-alone windows executable

Peachpie

http://www.peachpie.io

https://github.com/iolevel/peachpie

Peachpie is PHP 7 compiler based on Roslyn by Microsoft and drawing from popular Phalanger. It allows PHP to be executed within the .NET/.NETCore by compiling the PHP code to pure MSIL.

Phalanger

http://v4.php-compiler.net/

http://wiki.php-compiler.net/Phalanger_Wiki

https://github.com/devsense/phalanger

Phalanger is a project which was started at Charles University in Prague and was supported by Microsoft. It compiles source code written in the PHP scripting language into CIL (Common Intermediate Language) byte-code. It handles the beginning of a compiling process which is completed by the JIT compiler component of the .NET Framework. It does not address native code generation nor optimization. Its purpose is to compile PHP scripts into .NET assemblies, logical units containing CIL code and meta-data.

Bambalam

https://github.com/xZero707/Bamcompile/

Bambalam PHP EXE Compiler/Embedder is a free command line tool to convert PHP applications to standalone Windows .exe applications. The exe files produced are totally standalone, no need for php dlls etc. The php code is encoded using the Turck MMCache Encode library so it's a perfect solution if you want to distribute your application while protecting your source code. The converter is also suitable for producing .exe files for windowed PHP applications (created using for example the WinBinder library). It's also good for making stand-alone PHP Socket servers/clients (using the php_sockets.dll extension).
It's NOT really a compiler in the sense that it doesn't produce native machine code from PHP sources, but it works!

ZZEE PHPExe

http://www.zzee.com/phpexe/

ZZEE PHPExe compiles PHP, HTML, Javascript, Flash and other web files into Windows GUI exes. You can rapidly develop Windows GUI applications by employing the familiar PHP web paradigm. You can use the same code for online and Windows applications with little or no modification. It is a Commercial product.

phc-win

http://wiki.swiftlytilting.com/Phc-win

The PHP extension bcompiler is used to compile PHP script code into PHP bytecode. This bytecode can be included just like any php file as long as the bcompiler extension is loaded. Once all the bytecode files have been created, a modified Embeder is used to pack all of the project files into the program exe.

Requires

  • php5ts.dll
  • php_win32std.dll
  • php_bcompiler.dll
  • php-embed.ini

ExeOutput

http://www.exeoutput.com/

Commercial

WinBinder

http://winbinder.org/ - no longer available

(original: https://github.com/stefan-loewe/WinBinder)

(fork: https://github.com/wagy/WinBinder)

WinBinder is an open source extension to PHP, the script programming language. It allows PHP programmers to easily build native Windows applications, producing quick and rewarding results with minimum effort. Even short scripts with a few dozen lines can generate a useful program, thanks to the power and flexibility of PHP.

PHPDesktop

https://github.com/cztomczak/phpdesktop

PHP Desktop is an open source project founded by Czarek Tomczak in 2012 to provide a way for developing native desktop applications using web technologies such as PHP, HTML5, JavaScript & SQLite. This project is more than just a PHP to EXE compiler, it embeds a web-browser (Internet Explorer or Chrome embedded), a Mongoose web-server and a PHP interpreter. The development workflow you are used to remains the same, the step of turning an existing website into a desktop application is basically a matter of copying it to "www/" directory. Using SQLite database is optional, you could embed mysql/postgresql database in application's installer.

PHP Nightrain

https://github.com/kjellberg/nightrain

Using PHP Nightrain you will be able to deploy and run HTML, CSS, JavaScript and PHP web applications as a native desktop application on Windows, Mac and the Linux operating systems. Popular PHP Frameworks (e.g. CakePHP, Laravel, Drupal, etc…) are well supported!

phc-win "fork"

https://github.com/RDashINC/phc-win

A more-or-less forked version of phc-win, it uses the same techniques as phc-win but supports almost all modern PHP versions. (5.3, 5.4, 5.5, 5.6, etc) It also can use Enigma VB to combine the php5ts.dll with your exe, aswell as UPX compress it. Lastly, it has win32std and winbinder compilied statically into PHP.

EDIT

Another option is to use

http://www.appcelerator.com/products/titanium-cross-platform-application-development/

an online compiler that can build executables for a number of different platforms, from a number of different languages including PHP

TideSDK

http://www.tidesdk.org/

TideSDK is actually the renamed Titanium Desktop project. Titanium remained focused on mobile, and abandoned the desktop version, which was taken over by some people who have open sourced it and dubbed it TideSDK.

Generally, TideSDK uses HTML, CSS and JS to render applications, but it supports scripted languages like PHP, as a plug-in module, as well as other scripting languages like Python and Ruby.



Related Topics



Leave a reply



Submit