Include PHP Classes Auto-Magically

This has to be one of my favorite time savers.

If you’re an experienced PHP programmer you separate your common methods (aka: functions) into Class files.  Over time, this can become an organizational nightmare if your application became large.

There is an easy fix for this using PHP’s spl_autoload_register.  This not so well known function is called each time a Class is instantiated.  We can register our own functions to process the name of the Class being called, and easily include that Class in real-time.

Using this approach could not be any simpler.

Assuming you have included the SPL_AUTOLOADER scripts first, instantiating myClass() will auto-magically load the Class file myClass.php from the defined directory set in the myAutoloader() function.

It’s so simple you’ll ask yourself why you have not used this in the past.

Strip UTF-8 characters invalid for use in XML markup

When you put UTF-8 encoded strings into an XML document you should remember that not all UTF-8 characters are accepted in an XML document

You should strip away the invalid characters, else you’ll have an XML fatal parsing error tossed during parsing.

It’s rather easy to accomplish this using the following function.


Using PHP Context Streams to FTP files onto another server.

Here I’m going to show you how to use PHP streams to connect to an FTP server, create a new file and place content into this file. We also need an additional stream context telling PHP to overwrite the file if it exists.  A common stumble I see in scripts like this all the time.

You could also assign to the $content value the entirety of a file on your server using file_get_content() or perhaps a generated CVS or JSON or XML data file.

Auto-Loading PHP Script Widgets

The architecture for an application I created for a client required a method to auto-load a set of widgets (aka: individual PHP scripts) located in the “widgets” directory on the server.  This solution worked perfectly.

Of course, you can enhance this script by refactoring it into a function and adding additional filters to allow only certain types of files to load, like those prefixed with “widget-“.

Create Table Columns from an Array of Data using PHP

Recently I needed to take a list of URL’s from a database and present them in an HTML table.  I needed to split the list into an equal number of list items per column in the table.

The PHP script function below accepts a single array of items which could be URL’s, words, phrases, or some other text and a value for the number of columns your table should contain.


Making strings safe for use in a URL with PHP

These days taking advantage of Search Engine Optimization (SEO) by packing the URL with a string of words is commonplace but not all strings are safe to use in a URL.  The following PHP script function will prepare your string for use in a URL.


Delete all files matching patter using PHP glob()

When you have the need to do a little house cleaning on your storage system programmatically you can use PHP’s glob() and array_map() functions to make easy work of this.

In this example, we are going to find all text files in “some/dir/” and unlink (aka: delete) them. The PHP glob() function gets us our list of files and then the array_map() function passes each index value to the callback function, in this case, the “unlink” system command.


array_map(callback, array)



The result of this

Get a list of files or directories using PHP glob()

The PHP glob() function returns an array of filenames or directories matching a specified pattern.

This function returns an array of files or directories or FALSE on failure.




The output of the code above could be:


Checking for Oversized Uploads using PHP

It’s always good practice to validate the uploads that you allow from the end-user are not exceeding your application or system limitations.

Here is an example of how to check if an uploaded file exceeds the default limit value held in your php.ini configuration.


Force UTF-8 onto a MySQL Table

I have come across an instance where converting to UTF8 during the MySQL import process does not always “convert to UTF8” even though the table charset is set to UTF8.  The following query will fix a column with data stored latin1 in a utf8 table: