Archives: May 2014
The first step is to find the right executable. For example, the default PHP executable is still in version 5.3.16:
$ php -v [...] PHP 5.3.16 (cgi-fcgi) (built: Aug 27 2012 17:36:50) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
Other versions are available and we have to specify them explicitly, as we do for Apache through an .htaccess file. My scripts need at least PHP 5.4 and, to use it in CLI, we need the following executable:
$ php.ORIG.5_4 -v Fatal error: Directive 'allow_call_time_pass_reference' is no longer available in PHP in Unknown on line 0
OK, we found it but it crashes right away because of the default configuration. To make it work properly, we need to add a flew flags:
$ php.ORIG.5_4 -d allow_call_time_pass_reference=0 -d magic_quotes_gpc=0 -d register_globals=0 /path/to/script.php
To add some context, my goal here was to insert a copyright statement at the start of each PHP file present in my working directory. I knew how to replace the content of a file with the sed utility, so I first started to look for a way to read input from a file.
It appears that the GNU version of sed can do it by using the r option, which stands for "read". So to make sed insert the content of a file named header after the first line of the PHP file source.php:
$ sed '1r header' source.php > source.php.temp
The use of redirections force us to create a temporary file. Redirecting directly to source.php would give us an empty file! Fortunately, there is a utility called sponge, from a collection of tools called moreutils1, which allows us to redirect the modified content to the same source.php file:
$ sed '1r header' source.php | sponge source.php
Now, all there is left to do is executing that command on each PHP file present in the main directory and all its subfolders. For that, I just wrote a for loop in a bash script but I learned how to select all the files recursively using the following glob pattern:
This is available from bash v4 and to use it in a script, just add the following line before:
shopt -s globstar
1 The moreutils package is available on Arch Linux in the community repository.
The Unconditional Basic Income could be a solution to end poverty and ensure a decent life to everyone. We have to be enough asking The European Commission for them to consider that option seriously.
Please help make pressure on UN and South Sudan neighbours to protect the people of South Sudan from genocide.