This is an HOWTO that explain how you can create a Windows binary that will run a real PHP script.
There is no magic here, scripts are stored and accessed the same way icons, HTML pages and other binary are usually stored and accessed in Windows executables.
Please look at the MSDN for more info on resources, but a simple quote explain it all:
A resource is binary data that you can add to the executable file of a Windows-based application. A resource can be either standard or defined. The data in a standard resource describes an icon, cursor, menu, dialog box, bitmap, enhanced metafile, font, accelerator table, message-table entry, string-table entry, or version information. An application-defined resource, also called a custom resource, contains any data required by a specific application.
Note: It is possible to update
the resources of a Windows-binary using the win32std resource API.
You can also find on this wiki a simple command line utility named Embeder that can be used to create a Windows executable without a compiler.
This HOWTO is for Php4. For Php5 some simple modifications are needed.
If you don’t have it and still want to create a PHP standalone APP look at Embeder.
c:\php\php-src
and c:\php\php-bin
)c:\php\php-bin\extensions
dirc:\php\php\
c:\php\php\Zend
c:\php\php\main
c:\php\php\TSRM
c:\php\php\SAPI\EMBED
c:\php\php-bin
c:\php\php-bin
to your pathc:\php\php-bin\dlls
to c:\php\php-bin\
or add c:\php\php-bin\dlls
to your pathconfigure
1) build system) or create a php.ini file that include the win32std
extension and put it in the same path than the exe or in the base path of your project when debugging.
run.php
Multithreaded DLL
or Debug Multithreaded DLL
is selected.compiling
it with bcompiler).
This is the main application source file. It ask PHP to include a resource named res:///PHP/RUN
.
//======================================================================== // Embeding a PHP source into a Windows executable file // // Author: Eric Colinet <e dot colinet at laposte dot net> // http://wildphp.free.fr/wiki/doku.php?id=howto:embeding_a_php_source_into_a_windows_executable_file //======================================================================== /* Standards includes */ #include <stdio.h> #include <math.h> /* PHP Defines */ #define ZEND_WIN32 #define PHP_WIN32 #define ZTS 1 #define ZEND_DEBUG 0 /* PHP Includes */ #ifdef __cplusplus extern "C" { #endif #include <php.h> #include <php_embed.h> #ifdef __cplusplus } #endif /* Main */ int main(int argc, char** argv) { zval ret_value; void ***tsrm_ls; int exit_status; char *eval_string= "include 'res:///PHP/RUN';"; // res:/// uses current image file, you can use res://some\path\to\file.exe/PHP/RUN php_embed_init(argc, argv PTSRMLS_CC); zend_first_try { PG(during_request_startup) = 0; zend_eval_string(eval_string, &ret_value, "main" TSRMLS_CC); exit_status= Z_LVAL(ret_value); } zend_catch { exit_status = EG(exit_status); } zend_end_try(); php_embed_shutdown(TSRMLS_C); return exit_status; }
This is the PHP script file used in this example. Feel free to make your own
<? //======================================================================== // Embeding a PHP source into a Windows executable file // // Author: Eric Colinet <e dot colinet at laposte dot net> // http://wildphp.free.fr/wiki/doku.php?id=howto:embeding_a_php_source_into_a_windows_executable_file //======================================================================== ?> <?=basename($argv[0])?> - Powered by PHP version <?=phpversion()."\n"?> <? /* Action list */ $actions= array( 'info' => 'phpinfo', 'env' => 'show_env', ); /* Action functions */ function show_env() { print_r($_SERVER); } /* Run specified action */ if( !isset($argv[1]) ) die( "Please specify something to do.\nUsage: {$argv[0]} action [params...]\nWhere action can be: ".implode(', ', array_keys($actions))."\n"); foreach( $actions as $k => $v ) { if( $k==$argv[1] ) { $params= $argv; array_shift($params); array_shift($params); call_user_func_array($v, $params); die("\n"); } } echo "Unknown action '{$argv[1]}'\n"; ?>
Minimal php.ini file needed for this exemple to work.
extension_dir = "c:\php\php-bin\extensions" extension=php_win32std.dll