Category: PHP

Information and howto articles for PHP

PHP is a powerful open source server-sided scripting language that can be used on a wide variety of operating systems and webservers, including Apache on UNIX and IIS on Windows.

PHP contains numerous extensions which enable you to dynamically create images and flash files, parse xml, connect to remote servers using pop, imap, ftp, http and raw sockets, natively access many database servers including PostgreSql, MySQL and Microsoft SQL Server, compress and decompress files, lookup domain names and and much more.

PHP has an excellent manual and online documentation which covers every function for every extension available.

Locale issues with Zend Lucene search

We use Zend Lucene Search on a PHP ecommerce website and ran into some issues where records added to the search index from the website interface weren’t the same as when created from the command line. It turned out to be a locale issue and setting the locale fixed the problem.

Error messages

A clue that something wasn’t working correctly was this error message:

iconv(): Detected an illegal character in input string

There are a variety of conversions that can be done which fix this error, but our problem appears to have been caused by a non-configured locale, defaulting to “C” and which does not support UTF8.

Check what locale is currently being used

In PHP, you can call “setlocale(LC_ALL, 0)” to find out what the locale is currently set as. Running through Nginx with PHP-FPM, it output this:

C

and from the command line this:

en_NZ.UTF-8

Running “locale” from a SSH terminal session output this:

LANG=en_NZ.UTF-8
 LANGUAGE=
 LC_CTYPE="en_NZ.UTF-8"
 LC_NUMERIC="en_NZ.UTF-8"
 LC_TIME="en_NZ.UTF-8"
 LC_COLLATE="en_NZ.UTF-8"
 LC_MONETARY="en_NZ.UTF-8"
 LC_MESSAGES="en_NZ.UTF-8"
 LC_PAPER="en_NZ.UTF-8"
 LC_NAME="en_NZ.UTF-8"
 LC_ADDRESS="en_NZ.UTF-8"
 LC_TELEPHONE="en_NZ.UTF-8"
 LC_MEASUREMENT="en_NZ.UTF-8"
 LC_IDENTIFICATION="en_NZ.UTF-8"
 LC_ALL=

which would indicate the CLI script is picking the locale from the system, but Nginx/PHP-FPM is not.

How to fix

It’s possible to set the default locale in the php.ini file, although it might not be a good idea if you run many websites on your server as it could cause issues.

Instead use the setlocale() to set it specifically for your website. Check out the www.php.net/setlocale manual page for more information about the function.

In my case, we did this:

setlocale(LC_ALL, 'en_NZ.UTF-8');

This then solved the issue with adding documents to the Lucene index.

Read More »

PHP: Class XsltProcessor not found

A PHP install on Debian 5 by default does not include the Xslt extension which means the following error will occur if trying to use the XsltProcessor class: " Fatal error: Class ‘XsltProcessor’ not found…". This post shows how to install the extension on Debian 5.

Read More »

PHP: Write and append to files with file_put_contents

The PHP file_put_contents() function is a useful shortcut for dumping a string into a file (compared with using fopen, fputs etc). By default it will create a new file if it does not exist, or overwrite the file if it does exist. Recently I re-read the documentation for this function and discovered it is also possible to append to a file with file_put_contents.

Read More »

Install PHP without Apache on Debian

When you go to install PHP on a Debian server, it will attempt to install a whole bunch of Apache packages instead. This is not very useful if you want to install a different web server, or no web server at all.

Read More »

OSX Quick Tip: How to remove automator services

The Automator in OSX allows you to create processes, services, folder actions etc. Once you have created a service in Automator there doesn’t appear to be a way to delete it in the Automater app.

Delete an Automator Service

To delete an automator service, open up the finder and navigate your way to you own Library then Services folder. The files are there with the name you gave it in automater. If the file extension is visible it will be “.workflow”.

So for example, if you created a service to hide dot files and had called it “Hide dot files” then it would show as “Hide dot files.workflow”.

Read More »

PHP Quick Tip: use __DIR__ instead of dirname(__FILE__) from PHP 5.3

The traditional way of getting the directory the current file is in with PHP is to use dirname(__FILE__). From PHP 5.3 there is a new magic constant__DIR__ which is the equivilent of this function call.

__DIR__ magic constant

The __DIR__ magic constant is a “magic constant” which changes depending where it is used. It contains the directory of the file it is in. If the file is an include, it is the directory that include file is in. If it is the main script it is the directory that script is in.

__DIR__ is available from PHP 5.3 only. If prior versions a notice will be issued if it is used and the value will be __DIR__ and not the actual directory.

Example error if a version prior to PHP 5.3 is used:

Notice: Use of undefined constant __DIR__ - assumed '__DIR__' in ...

Unless your application requires the use of PHP 5.3 or greater, stick with the traditional use of:

dirname(__FILE__);
Read More »